2

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?

4
  • 2
    The behaviour of json_encode did not change. But perhaps that of simplexml_load_string did. Commented Apr 11, 2014 at 11:27
  • 3
    This seems like a classic X/Y Problem to me: regardless of why this is behaving differently in different environments, converting an XML document blindly into JSON like this seems like a rather pointless exercise. What are you actually trying to achieve? Commented Apr 11, 2014 at 11:42
  • 1
    "Actually this xml is part of a very long xml. When I extract it as in this case, i get similar results on both version of php." <- So, the example you gave us doesn't actually reproduce the problem? That's not particularly helpful for debugging it. Commented Apr 11, 2014 at 11:44
  • @IMSoP I have edited the question and included the full xml. Commented Apr 14, 2014 at 6:23

1 Answer 1

3

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for finding the way to easily reproduce the issue and giving a solution. It does seem like a bug. I will check on bugs.php.net/report.php to see if similar issues have been reported and do the necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.