I searched stack overflow for this, and found an old similar question here:
Ignore comment node in SimpleXML [duplicate]
Unfortunately neither this nor its duplicates answer the question in my opinion.
Using this code:
$testXml = <<<XML
<root>
<comment>this comment is part of my payload and should be parsed</comment>
<node>
</node>
<!-- this comment should not be parsed-->
</root>
XML;
xmlDataTest = simplexml_load_string($testXml);
var_dump($xmlDataTest);
I get:
object(SimpleXMLElement)#401 (2) {
["comment"]=>
array(2) {
[0]=>
string(29) "this comment is part of my payload and should be parsed"
[1]=>
object(SimpleXMLElement)#403 (0) {
}
}
["node"]=>
object(SimpleXMLElement)#402 (0) {
}
}
But I would expect the commented-out-content to be completely ignored:
object(SimpleXMLElement)#401 (2) {
["comment"]=>
string(55) "this comment is part of my payload and should be parsed"
["node"]=>
object(SimpleXMLElement)#402 (0) {
}
}
Does anybody have an idea how to make simplexml_load_string
ignore the second comment?
EDIT due to comments concerning var_dump relevance.
If instead I want to quickly convert from XML to JSON I can do:
$json = json_encode(simplexml_load_string($testXml), JSON_PRETTY_PRINT);
Also here I get a different JSON depending on whether somebody put a comment in my XML or not. I either get nice and clean:
{
"comment": "this comment is part of my payload and should be parsed",
"node": {}
}
or ugly:
{
"comment": [
"this comment is part of my payload and should be parsed",
{}
],
"node": {}
}
Again I still feel it is very bad when comments change behaviour of simplexml_load_string
, though I know some of you will disagree. Anyway I can handle it, and I thank you all for your good comments so far (I'll dispense some upvotes)