php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: stefan dot priebsch at e-novative dot de
New email:
PHP Version: OS:

 

 [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)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-12 09:08 UTC] tony2001@php.net
  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>');
  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.
 [2007-07-13 08:08 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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"
encoding="UTF-8"?><test><sub>test</sub><another>value</another><path><to
><tag>something</tag></to></path></test>');
  var_dump(json_encode($xml));
  var_dump(json_encode((string) $xml));

  $xml = new SimpleXMLElement('<?xml version="1.0"
encoding="UTF-8"?><test><sub><![CDATA[test]]></sub><another>value</anoth
er><path><to><tag>something</tag></to></path></test>');
  var_dump(json_encode($xml));
  var_dump(json_encode((string) $xml));
 [2007-07-17 12:36 UTC] johannes@php.net
$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] jani@php.net
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] iliaa@php.net
In latest CVS given proper string casts the code returns the expected:

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

output.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC