If you have DOMNode in PHP, how can you get the outer xml (i.e. the all of the XML that is inside this element plus the element itself)?
For example, lets say this is the structure
<car>
<tire>Michelin</tire>
<seats>Leather</seats>
<type>
<color>red</color>
<make>Audi</make>
</type>
</car>
And I have a pointer to the <type> node... I want to get back
<type>
<color>red</color>
<make>Audi</make>
</type>
If I just ask for the text, I get back "redAudi".
You need a DOMDocument:
// If you don't have a document already:
$doc = new DOMDocument('1.0', 'UTF-8');
echo $doc->saveXML($node); // where $node is your DOMNode
Check DOMDocument::saveXML in the docs for more info.
When you pass a DOMNode to saveXML() you get back only the contents of that node not the whole document.
After an hour spent, I came up with this (it seems the best solution)
$node->ownerDocument->saveXml($node);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With