PHP: Convert XML into JSON.

This is a short guide on how to convert XML into JSON using PHP.

Personally, I prefer using JSON instead of XML. In my opinion, JSON is far less awkward to work.

Parsing XML with PHP has always been a pet hate of mine.

Take a look at the following PHP code, which contains an example XML string.

//A sample XML string.
$xml = "<?xml version='1.0'?>
<catalog>
   <book id='bk1'>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id='bk2'>
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>";

//Convert the XML string into an SimpleXMLElement object.
$xmlObject = simplexml_load_string($xml);

//Encode the SimpleXMLElement object into a JSON string.
$jsonString = json_encode($xmlObject);

//Convert it back into an associative array for
//the purposes of testing.
$jsonArray = json_decode($jsonString, true);

//var_dump out the $jsonArray, just so we can
//see what the end result looks like
var_dump($jsonArray);

If you run the snippet above, you will get the following PHP array structure.

PHP: XML to JSON

As you can see, it is an associative array that contains the “book” elements that were present in our XML string.

Note how the “id” attribute for each XML element is now accessible under the ‘@attributes‘ key.

To loop through each element, you can use a simple foreach loop.

//Using a PHP foreach loop to print out the ID and title
//of each book.
foreach($jsonArray['book'] as $book){
    echo $book['@attributes']['id'], ' - ' . $book['title'], '<br>';
}

The JSON string itself will look like this (you might want to copy and paste this into an online JSON formatter of some sort):

{"book":[{"@attributes":{"id":"bk1"},"author":"Gambardella, Matthew","title":"XML Developer's Guide","genre":"Computer","price":"44.95","publish_date":"2000-10-01","description":"An in-depth look at creating applications n with XML."},{"@attributes":{"id":"bk2"},"author":"Galos, Mike","title":"Visual Studio 7: A Comprehensive Guide","genre":"Computer","price":"49.95","publish_date":"2001-04-16","description":"Microsoft Visual Studio 7 is explored in depth,n looking at how Visual Basic, Visual C++, C#, and ASP+ are n integrated into a comprehensive development n environment."}]}

Note that this method of converting XML into JSON should work in most circumstances.

However, doing this kind of conversion can always be tricky. Make sure that you test the output before you presume that everything is working perfectly!