|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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"}}}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 10:00:01 2025 UTC |
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.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.