Is it possible to apply a SimpleXML object to a DOMDocument object in PHP?

As a continuation of my previous question, I am thinking of using simplexml_load_file to load an XML file from a URL.

Is it possible for me to turn this SimpleXML object into a DOMDocument object via (DOMDocument)$my_simplexml ?

+4
source share
2 answers

You can use the dom_import_simplexml function.


Citation:

 DOMElement dom_import_simplexml ( SimpleXMLElement $node ) 

This function takes a SimpleXML nodeclass SimpleXML and does so in a DOMElement node. This new object can then be used as a native DOMElement node.


And that’s exactly how he said that the complete opposite manipulation can be done with simplexml_import_dom .


Well, this is not a "throw"; it is a function call ... But I think that everything will be fine for you; -)

+5
source

As mentioned earlier, dom_import_simplexml () will return a DOMElement from which you can get the associated DOMDocument:

 $doc = dom_import_simplexml($my_simplexml)->ownerDocument; 

If you do not plan to use SimpleXML, you can load the document directly from the DOM.

 $doc = new DOMDocument; $doc->load($url); 
+4
source

Source: https://habr.com/ru/post/1308397/


All Articles