views:

49

answers:

2

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 me to turn that SimpleXML object to a DOMDocument object via (DOMDocument)$my_simplexml?

+3  A: 

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 a DOMElement node. This new object can then be used as a native DOMElement 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 ;-)

Pascal MARTIN
Well, this answers the whole type casting part. :-)
DKinzer
+3  A: 

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);
Josh Davis
wow, better yet!
DKinzer