|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54632 json_encode() doesn't convert SimpleXML data properly
Submitted: 2011-04-29 13:42 UTC Modified: 2015-05-23 21:30 UTC
Avg. Score:4.5 ± 0.9
Reproduced:200 of 243 (82.3%)
Same Version:142 (71.0%)
Same OS:139 (69.5%)
From: enrico at zimuel dot it Assigned:
Status: Open Package: SimpleXML related
PHP Version: 5.3.6 OS: Ubuntu Linux 10.04
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.
Block user comment
Status: Assign to:
Bug Type:
From: enrico at zimuel dot it
New email:
PHP Version: OS:


 [2011-04-29 13:42 UTC] enrico at zimuel dot it
I tried to encode the following XML document in JSON using json_encode() and the result doesn't reflect the XML structure.

XML document:
<?xml version="1.0" encoding="UTF-8" ?>
<a><b id="foo"/>bar</a>

Result of json_encode():

The JSON results lost the "bar" value of the XML document.

This is the source code that is used:
$xml='<?xml version="1.0" encoding="UTF-8" ?><a><b id="foo"/>bar</a>';
$simpleXML= simplexml_load_string($xml);
echo json_encode($simpleXML);

Test script:
$xml='<?xml version="1.0" encoding="UTF-8" ?><a><b id="foo"/>bar</a>';
$simpleXML= simplexml_load_string($xml);
echo json_encode($simpleXML);

Expected result:
It's not obviuos the correct JSON rappresentation of the above XML document.
In my opinion it can be as follow:

Where the value "bar" is included in a JSON array using the syntax [...]

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-29 16:20 UTC] felipecg00 at gmail dot com
Looks like the problem is not the json_encode.

var_dump( $simpleXML );

object(SimpleXMLElement)#1 (1) {
  object(SimpleXMLElement)#2 (1) {
    array(1) {
      string(3) "foo"

Where is bar? This is the behavior of SimpleXML. You can try using DOMDocument:

$dd = new DOMDocument();
$dd->loadXML('<?xml version="1.0" encoding="UTF-8" ?><a><b id="foo"/>bar</a>');
echo $dd->documentElement->textContent; // outputs bar

Still, you can force the SimpleXMLElement to be a string, so it will contain the text inside the node:

$bar = (string) $simpleXML;
echo $bar; // outputs 'bar'

And yet, there is dom_import_simplexml(), which converts a SimpleXML node into a DOMDocument node. But that i've never used before.

I think the right thing to do is to keep the text inside text-only nodes.
 [2011-05-02 15:35 UTC] enrico at zimuel dot it
I agree that the var_dump output of the SimpleXMLElement doesn't contains the text value of the <a> element. But, as you said, you can get it from $bar = (string) $simpleXML, that means the data is inside the SimpleXMLElement even if the values is not explicit. 
The problem here is related to the fact that there is not a standard way to convert an XML into JSON. IBM proposed the JSONx format. To continue to follow the syntax used by SimpleXMLElement we can add a special element (like '@attributes') to manage the text value.
For instance the xml document: <a><b id="foo"/>bar</a> can be translated in {"a":{"b":{"@attributes":{"id":"foo"}},"@text":"bar"}}.
So, the problem is in SimpleXMLElement? I don't know, for sure the result of json_encode() is not correct.
 [2012-01-02 01:30 UTC] php at keithtyler dot com
It's really too bad that there is not an analogous SimpleJSONElement object. It would seem very PHPian to be able to process and traverse XML and JSON the same way.
 [2015-05-23 21:30 UTC]
-Package: JSON related +Package: SimpleXML related
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Feb 17 17:01:25 2019 UTC