I get an XML string from a certain source. I create a DOMDocument object and load the XML string into it (with DOMDocument::loadXML()). Then I navigate through the XML doc using various methods (e.g. DOMXPath), until I find the node (a DOMNode, of course) that I want.
This node has a bunch of descendants, and I want to take that entire node (and its descendants) and create a new DOMDocument object from it. I'm not sure how to do this; I tried creating a new DOMDocument and using DOMDocument::importNode(), but this appears to only work if the DOMDocument already has a main document node in it, in which case it appends the imported node as a child of the main document node, which is not what I want -- I want the imported node to BECOME the DOMDocument main node.
Maybe there's an easier way to do this (i.e. an easier way to extract the part of the original XML that I want to turn into its own document), but I don't know of it. I'm relatively new to DOMDocument, although I've used SimpleXMLElement enough to be annoyed by it.
The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name.
$dom = new DOMDocument('1.0', 'utf-8'); $element = $dom->createElement('foo', 'me & you'); $dom->appendChild($element); echo $dom->saveXML();
PHP | DOMElement __construct() Function The DOMElement::__construct() function is an inbuilt function in PHP which is used to create a new DOMElement object.
A DomDocument is a container (variable/object) for holding an XML document in your VBA code. Just as you use a String variable to hold a strings value, you can use a DomDocument to hold an XML document. (for a complete list of a DomDocuments properties, see halfway down this page)
You can also create a new DOMDocument and call appendChild() on it to add a root node:
$new = new DomDocument; $new->appendChild($new->importNode($node, true));
That worked for me.
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