I'm experiencing the following behavior:
$xml_string1 = "<person><name><![CDATA[ Someone's Name ]]></name></person>";
$xml_string2 = "<person><name> Someone's Name </name></person>";
$person = new SimpleXMLElement($xml_string1);
print (string) $person->name; # Someone's Name
$person = new SimpleXMLElement($xml_string2);
print (string) $person->name; # Someone's Name
$person = new SimpleXMLElement($xml_string1, LIBXML_NOCDATA);
print (string) $person->name; # Someone's Name
The php docs say that NOCDATA "Merge[s] CDATA as text nodes". To me this means that CDATA will then be treated the same as text nodes - or that the behavior of the 3rd example will now be the same as the 2nd example.
I don't have control over the XML (it's a feed from an external source), otherwise I'd just remove the CDATA tag as it does nothing and ruins the behavior I want.
Why does the above example behave the way that it does? Is there any way to make SimpleXML handle the CDATA nodes in the same way that it handles text nodes? What does "Merge CDATA as text nodes" actually do, since I don't seem to be understanding that option?
I'm currently decoding after I pull out the data, but the above example still doesn't make sense to me.
See Question&Answers more detail:os