php.net |  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: 2021-08-11 09:56 UTC
Votes:252
Avg. Score:4.5 ± 0.9
Reproduced:203 of 246 (82.5%)
Same Version:142 (70.0%)
Same OS:142 (70.0%)
From: enrico at zimuel dot it Assigned:
Status: Verified Package: SimpleXML related
PHP Version: 5.3.6 OS: Ubuntu Linux 10.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: enrico at zimuel dot it
New email:
PHP Version: OS:

 

 [2011-04-29 13:42 UTC] enrico at zimuel dot it
Description:
------------
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():
{"b":{"@attributes":{"id":"foo"}}}

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

This is the source code that is used:
<?php
$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:
{"a":[{"b":{"@attributes":{"id":"foo"}}},"bar"]}

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

Actual result:
--------------
{"b":{"@attributes":{"id":"foo"}}}

Patches

Pull Requests

History

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) {
  ["b"]=>
  object(SimpleXMLElement)#2 (1) {
    ["@attributes"]=>
    array(1) {
      ["id"]=>
      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] cmb@php.net
-Package: JSON related +Package: SimpleXML related
 [2021-08-11 09:56 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-08-11 09:56 UTC] cmb@php.net
Still broken: <https://3v4l.org/VgfoH>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 17:01:27 2024 UTC