how can I convert a xml document to json without losing single tag elements?
My XML:
<myTag><singleTag internValue="bli bla blo"/></myTag>
my PHP:
$xml = simplexml_load_string($result);
$json = json_decode(json_encode($xml));
my output/result:
myTag = Object
(
0 = Object
(
@attributes = Object
(
internValue = String(11) "bli bla blo"
)
)
)
but I am missing the information about the Name "singleTag". It does not appear in my result, but why? After all the "myTag" name is displayed.
I tried different solutions, but the problem is in the json_encode. Already here the information is lost:
"myTag":{"0":{"@attributes":{"internValue":"bli bla blo"}}}
thanks for any help.
greetings,
christopher2007
EDIT:
Here is a better example of the problem:
$result = '<surround>
<mainCat>
<firstTag val1="false" val2="true" val3="false" val4="false" />
<secondTag val1="false" val2="true" val3="false" val4="false" />
<myTag><singleTag internValue="bli bla blo"/></myTag>
</mainCat>
</surround>';
$xml = simplexml_load_string($result);
$json = json_decode(json_encode($xml));
Solution:
The problem lay in the PHP Version. At the beginning i Had Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3 and with a downgrade to PHP Version 5.6.13 everything worked fine.
So thanks again for all your help and sorry for such a trivial error :/