2

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 :/

toddeTV
  • 1,447
  • 3
  • 20
  • 36
  • The example you give is *not* reproduceable with any currently supported PHP version: https://3v4l.org/NNapP - It's therefore unclear what you're asking for. Which PHP version is that? – hakre Sep 23 '15 at 17:34
  • im also having same issue. working fine in php7.0.30-0ubuntu0.16.04.1. but 'singleTag' is becoming 0 in php5.6.4-4ubuntu6.4 – Santhy K May 22 '18 at 09:25
  • while converting xsd atom to array. Any resolution found for this? – Santhy K May 22 '18 at 11:40

2 Answers2

3

Try this

$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

EDIT :

To iterate over a multidimensional array, you can use RecursiveArrayIterator

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}
sbouaked
  • 955
  • 10
  • 29
  • if I try that code it gives `Array ( [singleTag] => Array ( [@attributes] => Array ( [internValue] => bli bla blo ) ) ) ` – Niki van Stein Sep 23 '15 at 07:58
  • I think it's the way you read your JSON who is not correct, take a look at this : https://lostechies.com/seanbiefeld/2011/10/21/simple-xml-to-json-with-php/ `` – sbouaked Sep 23 '15 at 08:01
  • i also visited this site before and tried to format the string, sithout success. But you are right, the problem is in the xml convert and not the json_encode. a normal "var_dump" on the "$xml" shows this: `["myTag"]=> object(SimpleXMLElement)#19 (1) { [0]=> object(SimpleXMLElement)#20 (1) { ["@attributes"]=> array(1) { ["internValue"]=> string(12) "bli bla blo" } } }` – toddeTV Sep 23 '15 at 08:09
  • I found a solution from @FTav called "xml2js" that workes in here: http://stackoverflow.com/questions/8830599/php-convert-xml-to-json But I can not use it because the generated json is completly different to the normal "json_en/decode" and my code is a little bit longer to change this fundamental thing. But why is it working in his function and not in the normal php one? I am so confused right now ... – toddeTV Sep 23 '15 at 08:22
  • http://stackoverflow.com/a/8845727/2323245 json_encode handles objects differently than strings.you can cast the object to a string . – sbouaked Sep 23 '15 at 08:24
  • @BasvanStein i updated my question with an better example. Here you can see the problem I have. – toddeTV Sep 23 '15 at 08:33
1

This should actually be a comment, but it is too long for a comment:

I execute your code given in the question

$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));

echo json_encode($xml);
echo "<br/>print_r: <br/>";
print_r($json);

at http://phptester.net and get the following result

{"mainCat":{"firstTag":{"@attributes":{"val1":"false","val2":"true","val3":"false","val4":"false"}},"secondTag":{"@attributes":{"val1":"false","val2":"true","val3":"false","val4":"false"}},"myTag":{"singleTag":{"@attributes":{"internValue":"bli bla blo"}}}}}
print_r: 
stdClass Object ( [mainCat] => stdClass Object ( [firstTag] => stdClass Object ( [@attributes] => stdClass Object ( [val1] => false [val2] => true [val3] => false [val4] => false ) ) [secondTag] => stdClass Object ( [@attributes] => stdClass Object ( [val1] => false [val2] => true [val3] => false [val4] => false ) ) [myTag] => stdClass Object ( [singleTag] => stdClass Object ( [@attributes] => stdClass Object ( [internValue] => bli bla blo ) ) ) ) )

No information is lost as far as I see, your singleTag is right there.

So perhaps it is a PHP version problem? What version of PHP are you using?

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • this is killing me :D I now made a new, blank file and copied the EXACT same code in it, that you were using. And still: instead of "singleTag" I get a "0". But when I duplicate the "singleTag" Tag inside the "myTag", it is working, what of course is no option for the original code, but still funny ... At the moment, I am using a localhost apache on a win7 for developing: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3 .... I now also tried it on a Ubuntu with PHP Version 5.2.17 and there it works ... So you my friend had a very good idea. But is it the PHP Version? or the os? or the apache? – toddeTV Sep 23 '15 at 09:03
  • Most probably the PHP version, try to do `phpinfo` on both setups to see the differences. – Niki van Stein Sep 24 '15 at 07:36