Home » Php » php – Convert SimpleXML to DOMDocument, not DOMElement

php – Convert SimpleXML to DOMDocument, not DOMElement

Posted by: admin July 12, 2020 Leave a comment

Questions:

I need to convert a SimpleXML object to a DOMDocument, to use it with DOMXPath. I need DOMXPath so that I can use its registerPHPFunctions method. Can this be done?

With dom_import_simplexml I get a DOMElement, not DOMDocument.

How to&Answers:

You can fetch the document from $anyDOMNode->ownerDocument

Answer:

$dom=new DOMDocument;
$dom->loadXML($sxml->asXML());