2

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?

2
  • When an element is empty, there's no way for the parser to know if it would have contained a string or would have contained nested elements. It assumes nested elements when it's converting to JSON. If you need to control this, you should write your own code that creates arrays from the XML data, and puts empty strings there. Commented May 5, 2017 at 20:58
  • @Barmar Thank you for your comment. I will look into writing something on my own. Commented May 9, 2017 at 15:56

1 Answer 1

3

use xpath() to get empty nodes and unset() to remove them

working example:

$xml_string = <<<XML
<content>
    <row>
        <column>
            <item1></item1>
            <item2></item2>
        </column>
    </row>
    <row>
        <column>
            <item1>Item 1</item1>
            <item2>Item 2</item2>
        </column>
    </row>
</content>
XML;

$xml = simplexml_load_string($xml_string);
$xpath = '//*[not(normalize-space())]';

foreach (array_reverse($xml->xpath($xpath)) as $remove) {
    unset($remove[0]);
}

$json = json_encode($xml);

echo $json;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. However in my case if the element is not there in the response, it would trigger a different logic in my application. So can not remove the element from the response.

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.