When to use LIBXML_NOCDATA
?
I add the issue when transforming XML to JSON.
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo json_encode($xml, true);
/* prints
{
"content": {}
}
*/
When accessing the SimpleXMLElement object, It gets the CDATA :
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>");
echo $xml->content;
/* prints
Hello, world!
*/
I makes sense to use LIBXML_NOCDATA
because json_encode
don't access the SimpleXMLElement to trigger the string casting feature, I'm guessing a __toString()
equivalent.
$xml = simplexml_load_string("<foo><content><![CDATA[Hello, world!]]></content></foo>", null, LIBXML_NOCDATA);
echo json_encode($xml);
/*
{
"content": "Hello, world!"
}
*/