|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-07-12 07:39 UTC] stefan dot priebsch at e-novative dot de
Description:
------------
When json_encoding (Simple)XML data, CDATA sections are ignored.
Reproduce code:
---------------
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test>test</test>');
var_dump((string) $xml);
var_dump(json_encode($xml));
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test><![CDATA[test]]></test>');
var_dump((string) $xml);
var_dump(json_encode($xml));
Expected result:
----------------
string 'test' (length=4)
string '{"0":"test"}' (length=12)
string 'test' (length=4)
string '{"0":"test"}' (length=12)
Actual result:
--------------
string 'test' (length=4)
string '{"0":"test"}' (length=12)
string 'test' (length=4)
string '{}' (length=2)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 23 11:00:02 2025 UTC |
Sorry, I disagree. This is a bug, not bogus. The manual says that json_encode works on any type except resources. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test><![CDATA[test]]></test>'); var_dump(is_resource($xml)); outputs false, (which is obvious as SimpleXML is not listed in the list of resource types). Thus SimpleXML is not a resource and must be processed by json_encode() without a string cast. How would I supposed to cast an XML tree to string anyway? Please have a look at bug#38680, where a similar issue was discussed.