<node1>
<node2>
<node3>
</node3>
<node3>
</node3>
<node3>
</node3>
</node2>
<node2>
<node3>
</node3>
<node3>
</node3>
<node3>
</node3>
</node2>
...
</node1>
Let’s say I have this structure in an XML document. I would like to be able to comment a node and all its content and to uncomment it if necessary, using PHP. I tried to find a way looking at DOMDocument’s documentation and SimpleXML’s documentation without success.
Edit: Just to clarify: I found how to comment a node, but not how to uncomment it.
Comments can be created using
DOMDocument::createComment(). Replacing comments with actual nodes is just like replacing any other node type, useDOMElement::replaceChild().The (super-simplified) examples above should output something like:
Reference
DOMDocument::createComment()to create<-- comments -->DOMElement::replaceChild()to swap nodes aroundDOMDocument::createDocumentFragment()to create a document fragmentDOMDocumentFragment::appendXML()to append a string of xml to a fragmentDOMDocument::saveXML()to get the XML for an elementDOMCharacterData->datato get the content of a comment