Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 349
dkinzer Avatar asked Jan 22 '23 04:01

dkinzer


1 Answers

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 ;-)

like image 82
Pascal MARTIN Avatar answered Jan 29 '23 14:01

Pascal MARTIN