I'm currently processing an extensive XML file, to make some of the processing easier I've used the following method as mentioned extensively on stack overflow
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
This has been awesome but going over my code I've noted some instances where attributes on certain elements aren't converting correctly, at this step $json = json_encode($xml);
Here is a stripped down XML example.
<?xml version="1.0"?>
<property>
<landDetails>
<area unit="squareMeter"/>
</landDetails>
<buildingDetails>
<area unit="squareMeter">100</area>
</buildingDetails>
</property>
and here is the output.
Array (
[landDetails] => Array (
[area] => Array (
[@attributes] => Array (
[unit] => squareMeter
)
)
)
[buildingDetails] => Array (
[area] => 100
)
)
As seen above if the element contains any info on that exact node the associated attributes with that element are not processed. This is causing significant data loss between the conversion.
Does anyone know how to solve this issue?
Thanks in advance!