Reputation: 497
I have the following xml
:
$resultXML=<<<XML
<Mapper ProviderCode="" ProviderName="" SessionID="" Supplier="" ProviderSupplier="">
<ProviderSupplierName/>
</Mapper>
XML;
When I issue:
$sxml = simplexml_load_string($resultXML);
echo json_encode($sxml);
in PHP Version 5.3.3-7+squeeze15
, I get:
"Mapper": {
"@attributes": {
"ProviderCode": "",
"ProviderName": "",
"SessionID": "",
"Supplier": "",
"ProviderSupplier": ""
},
"ProviderSupplierName": {}
}
while in PHP Version 5.4.4-14+deb7u8
, I get:
"Mapper": {
"0": {},
"@attributes": {
"ProviderCode": "",
"ProviderName": "",
"SessionID": "",
"Supplier": "",
"ProviderSupplier": ""
}
}
I want to use PHP Version 5.4.4-14+deb7u8
and get same result as PHP Version 5.3.3-7+squeeze15
.
Actually this xml is part of a very long xml. When I extract it as in this case, i get similar results on both versions of php.
The full xml can be found at https://drive.google.com/file/d/0B9zOzk6voXHKQ3N1ZklfVFVITFU/edit?usp=sharing
Another thing I noticed is that if I use the xml as given in link, I get the json as in the second example, but if I use an xml formatter and format the xml, then I get the json in the first example.
Any help?
Upvotes: 2
Views: 208
Reputation: 146588
You just need the <ProviderSupplierName/>
tag to reproduce the issue and, as you already mention, the different output happens when you reformat the source XML and add white space around it:
<Hotels>
<Hotel>
<Mapper><ProviderSupplierName/></Mapper>
</Hotel>
</Hotels>
{"Hotel":{"Mapper":{"0":{}}}}
<Hotels>
<Hotel>
<Mapper>
<ProviderSupplierName/>
</Mapper>
</Hotel>
</Hotels>
{"Hotel":{"Mapper":{"ProviderSupplierName":{}}}}
To get coherent pre-PHP/5.4 automated parsing you can provide the LIBXML_NOBLANKS flag («Remove blank nodes»):
$sxml = simplexml_load_string($resultXML, 'SimpleXMLElement', LIBXML_NOBLANKS);
(It's hard to say whether the change is intentional but a vanishing tag name feels like a bug.)
Upvotes: 3