Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Parse An XML Response With PHP

TwitterFacebookRedditLinkedInHacker News

If you’re like me, you find XML a real pain to deal with, but yet it still seems to exist with various web services. If you’re using Android or AngularJS, these frameworks can’t process XML out of the box, but they can JSON.

With the assistance of a PHP powered web server, you can easily transform the nasty XML responses you get into something more usable like JSON.

PHP has a nifty method called simplexml_load_string and what it does is it loads an XML structured string into an easy to use object. Let’s say you have a string variable called $xmlResponse that has the following properly formatted XML in it:

<?xml version='1.0'?>
<business>
    <company>Code Blog</company>
    <owner>Nic Raboy</owner>
    <employees>
        <employee>
            <firstname>Nic</firstname>
            <lastname>Raboy</lastname>
        </employee>
        <employee>
            <firstname>Maria</firstname>
            <lastname>Campos</lastname>
        </employee>
    </employees>
</business>

Doing the following in PHP will give us a very nice object to work with:

$xml = simplexml_load_string($xmlResponse);

Now to do the full conversion from object to JSON, I will be using ZendFramework 2. However, manipulations of the XML object can be done without a special framework or tool.

Let’s start by creating an array of our employees:

$employees = array();
foreach($xml->employees as $employee) {
    $employeeObject = array(
        "firstname" => $employee->firstname,
        "lastname" => $employee->lastname
    );
    array_push($employees, $employeeObject);
}

The above code will create a custom object for each employee in the XML and append them to an array. This will leave us with an array of employee objects customized to our liking.

The next thing we want to do is add the employees array to a custom object that has the rest of our business information in it:

$jsonObject = array(
    "company" => $xml->company,
    "owner" => $xml->owner,
    "employees" => $employees
);

Like I mentioned previously, I use ZendFramework 2 so I’ll be converting our custom object using the included Json methods. To convert to JSON, you would make a call like the following:

Json::encode($jsonObject);

If everything went well, the JSON result of our XML response should look something like this:

{
  "company": "Code Blog",
  "owner": "Nic Raboy",
  "employees": [
    {
      "firstname": "Nic",
      "lastname": "Raboy"
    },
    {
      "firstname": "Maria",
      "lastname": "Campos"
    }
  ]
}

If you have your web server display the JSON response instead of the raw XML, your clients are going to have a much easier time absorbing it.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.