Is it possible to cast a SimpleXML object to a DOMDocument object in PHP?
As a follow up to my earlier question, I'm thinking of using simplexml_load_file
to load an XML file from a URL.
Would it be possible for开发者_JS百科 me to turn that SimpleXML
object to a DOMDocument
object via (DOMDocument)$my_simplexml
?
You can use the dom_import_simplexml
function.
Quoting :
DOMElement dom_import_simplexml ( SimpleXMLElement $node )
This function takes the node node of class
SimpleXML
and makes it into aDOMElement
node. This new object can then be used as a nativeDOMElement
node.
And, just so it's said, the exact opposite manipulation can be done using simplexml_import_dom
.
Well, it's not a "cast" ; it's a function-call... But I guess that would still be OK for your ;-)
As previously mentionned, dom_import_simplexml() will return a DOMElement, from which you can get the related DOMDocument:
$doc = dom_import_simplexml($my_simplexml)->ownerDocument;
If you don't plan to actually use SimpleXML though, you can load the document directly from DOM.
$doc = new DOMDocument;
$doc->load($url);
精彩评论