Unfortunately, SimpleXML does not process comments. As already mentioned, the DOM processes comments, but compared to SimpleXML, this is a kind of useless activity for simple things.
My recommendation: try SimpleDOM . This is a SimpleXML extension, so everything works the same way, and it has tons of useful methods for working with the DOM.
For example, insertComment($content, $mode) can append or insert comments before or after given node. For example:
include 'SimpleDOM.php'; $root = simpledom_load_string('<root><value/></root>'); $root->value->insertComment(' mode: append ', 'append'); $root->value->insertComment(' mode: before ', 'before'); $root->value->insertComment(' mode: after ', 'after'); echo $root->asPrettyXML();
... will be an echo
<?xml version="1.0"?> <root> <value> </value> </root>
Josh davis
source share