views:

108

answers:

2

I neeed to convert a SimpleXML object to a DOMDocument, to use it with DOMXPath. I need DOMXPath for it's method registerPHPFunctions. Can this be done?

With dom_import_simplexml I get a DOMElement, not DOMDocument.

A: 
$dom=new DOMDocument;
$dom->loadXML($sxml->asXML());
stillstanding
Thanks! Just a note: The `loadXML()` method returns a boolean, and loads the XML to `$dom`. With this solution I will loose the connection to the original simple xml object, right? So say I remove the DOMElements later, this will not affect the simple xml.
Znarkus
code corrected. you still have access to $sxml represented here as your simplexml object. $dom and $sxml are independent of each other. whatever change you make in $dom will not affect $sxml. if you want the reverse of the above code:`$sxml=simplexml_load_string($dom->saveXML());`
stillstanding
Actually, when I'm converting with `dom_import_simplexml`, it preserves the connection to simple xml. If I remove a dom node, this change ripples back to simple xml.
Znarkus
dom_import_simplexml does exhibit that kind of behavior. notice that the code i showed you passes strings between $dom and $sxml, so the xml trees are sesparated.
stillstanding
+3  A: 

You can fetch the document from $anyDOMNode->ownerDocument

VolkerK
Thank you! I searched for this, but didn't find it. Exactly what I was looking for!
Znarkus