I am using SimpleXML to build a document, and wondering whether it is possible to insert comment tag to the document like this:
<root>
<!-- some comment -->
<value>
</root>
EDIT:
The comment is somewhere in the middle of the document.
<root>
<tag1 />
<!-- some comment -->
<value />
</root>
Unfortunately, SimpleXML doesn’t handle comments. As it’s been mentionned, DOM does handle comments but it’s a kind of a bother to use for simple stuff, compared to SimpleXML.
My recommendation: try SimpleDOM. It’s an extension to SimpleXML, so everything works the same and it has a bunch of useful methods to deal with DOM stuff.
For instance,
insertComment($content, $mode)canappendto or insert commentsbeforeoraftera given node. For example:…will echo