|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41976 json_encode() ignores CDATA in SimpleXML data
Submitted: 2007-07-12 07:39 UTC Modified: 2007-07-18 22:58 UTC
From: stefan dot priebsch at e-novative dot de Assigned:
Status: Closed Package: JSON related
PHP Version: 5.2.3 OS: WinXP
Private report: No CVE-ID: None
 [2007-07-12 07:39 UTC] stefan dot priebsch at e-novative dot de
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);

  $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test><![CDATA[test]]></test>');
  var_dump((string) $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)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-12 09:08 UTC]
  var_dump((string) $xml);
  var_dump(json_encode($xml)); <-- you have to cast it to string explicitly, otherwise json_encode() will encode the object itself, not it's string representation.
 [2007-07-13 06:33 UTC] stefan dot priebsch at e-novative dot de
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>');

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.
 [2007-07-13 08:08 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Yes, and it will try to serialize the SimpleXML object, what won't work, that's part of SimpleXML's behaviour 
 [2007-07-17 10:46 UTC] stefan dot priebsch at e-novative dot de
I re-opened the bug due to the discussion of bug #42001. json_encode() does not work on nested XML with a string cast, but it does work without a string cast. (Of course except for the CDATA problem that made me file this bug in the first place.)

$xml = new SimpleXMLElement('<?xml version="1.0"
  var_dump(json_encode((string) $xml));

  $xml = new SimpleXMLElement('<?xml version="1.0"
  var_dump(json_encode((string) $xml));
 [2007-07-17 12:36 UTC]
$xml and (string)$xml are different things. Using different things as arguments for a function gives different results.
 [2007-07-17 19:09 UTC] stefan dot priebsch at e-novative dot de
Johannes, would you please care to actually *read* and try to *understand* my bug report before marking it as bogus?

My point is that json_encode() works properly with nested XML, but does NOT work correctly when the nested XML contains CDATA.
 [2007-07-18 08:52 UTC]
Don't use such texts for 'Summary'. Please write something that describes the problem..
 [2007-07-18 15:59 UTC] stefan dot priebsch at e-novative dot de
Ok, the problem: I have a SimpleXML object that I want to convert to JSON using json_encode(). According to the manual, json_encode() works on all types.

As soon as there is CDATA in the XML, that very node shows up as "" in the JSON encoding. Remove the CDATA section, everything works. Thus I claim that json_encode() does not work properly on CDATA, which I consider a bug.
 [2007-07-18 22:58 UTC]
In latest CVS given proper string casts the code returns the expected:

string(4) "test"
string(6) ""test""
string(4) "test"
string(6) ""test""

PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jan 17 06:01:22 2021 UTC