I am having hard time consuming an api which has xml as response. I am using simplexml_load_string() to convert xml to php object and then with json_encode() to json response. When all the elements are filled like below
<content>
<row>
<column>
<item1>Item 1</item1>
<item2>Item 2</item2>
</column>
</row>
<row>
<column>
<item1>Item 1</item1>
<item2>Item 2</item2>
</column>
</row>
</content>
the json response will be correct like below
{
"content": {
"row": [
{
"column": [
{
"item1": "Item 1",
"item2": "Item 2"
},
{
"item1": "Item 1",
"item2": "Item 2"
}
]
},
{
"column": [
{
"item1": "Item 1",
"item2": "Item 2"
},
{
"item1": "Item 1",
"item2": "Item 2"
}
]
}
]
}
}
However when there is an empty item, json response for the corresponding xml will be different breaking my apis.
<content>
<row>
<column>
<item1></item1>
<item2></item2>
</column>
</row>
<row>
<column>
<item1>Item 1</item1>
<item2>Item 2</item2>
</column>
</row>
</content>
For the above xml json response will be
{
"row":[
{
"column":[
{
"item1":{
},
"item2":{
}
},
{
"item1":"Item 1",
"item2":"Item 2"
}
]
},
{
"column":[
{
"item1":"Item 1",
"item2":"Item 2"
},
{
"item1":"Item 1",
"item2":"Item 2"
}
]
}
]
}
Which is different than the previous one. Any help on how to overcome this please?