How to Convert XML Data to JSON in PHP
  John Mwaniki /   18 Dec 2023

How to Convert XML Data to JSON in PHP

XML (eXtensible Markup Language) and JSON (JavaScript Object Notation) are two popular formats used for structuring and transmitting data.

While XML has been a reliable format for a long time, JSON has gained more popularity due to its simplicity and compatibility with JavaScript.

In some scenarios, you may need to convert the data from one format to another. In this guide, you will learn how to convert XML data to JSON using PHP through multiple practical examples.

Converting XML Data to JSON in PHP

PHP provides a simple and effective way to manipulate both formats. Here are the steps for converting XML data to JSON:

Step 1. Loading the XML Data

The first step is to load the XML data into a PHP variable. We will use the SimpleXML extension, which simplifies the process of reading and manipulating XML.

There are two ways of doing it:

1. Using simplexml_load_string() function

This function parses the XML data from a string variable into a SimpleXMLElement object.

Example

<?php
$xmlString = '<person>
    <name>John Doe</name>
    <age>25</age>
</person>';

$xml = simplexml_load_string($xmlString);

2. Using simplexml_load_file() function

If the XML data is stored in a file, then you need to use this function to load it instead. It is a function that parses the XML data from a file into a SimpleXMLElement object.

Example

<?php
// Specify the file path in the function
$xml = simplexml_load_file("path/to/file.xml");

Step 2. Converting Data to JSON

Once we have the XML data loaded into a SimpleXMLElement object, we can then convert it to JSON using the json_encode() function.

This function takes a PHP variable, in this case, our SimpleXML object, and returns its JSON representation.

$json = json_encode($xml);

Example 1

<?php
$xmlString = '<person>
    <name>John Doe</name>
    <age>25</age>
</person>';

$xml = simplexml_load_string($xmlString);
$json = json_encode($xml);
echo $json;
?>

Output:
{"name":"John Doe","age":"25"}

Example 2

<?php
$students = <<<XML
<students>
    <student>
        <name>John Doe</name>
        <age>18</age>
        <marks>
            <maths>85</maths>
            <physics>80</physics>
            <chemistry>76</chemistry>
        </marks>
    </student>
    <student>
        <name>Mary Johnson</name>
        <age>17</age>
        <marks>
            <maths>78</maths>
            <physics>56</physics>
            <chemistry>87</chemistry>
        </marks>
    </student>
</students>
XML;

$xml = simplexml_load_string($students);
echo json_encode($xml);
?>

Output:
{"student":[{"name":"John Doe","age":"18","marks":{"maths":"85","physics":"80","chemistry":"76"}},{"name":"Mary Johnson","age":"17","marks":{"maths":"78","physics":"56","chemistry":"87"}}]}

In this example, we have used a more complex XML string with nested elements. The resulting JSON string has maintained the hierarchy and preserved the data structure.

In the instances where XML elements have attributes, they are stored in the array under the @attributes key.

<?php
$students = '<students semester = "1">
    <student position = "1">
        <name>John Doe</name>
        <age>18</age>
        <marks>
            <maths>85</maths>
            <physics>80</physics>
            <chemistry>76</chemistry>
        </marks>
    </student>
    <student position = "2">
        <name>Mary Johnson</name>
        <age>17</age>
        <marks>
            <maths>78</maths>
            <physics>56</physics>
            <chemistry>87</chemistry>
        </marks>
    </student>
</students>';

$xml = simplexml_load_string($students);
echo json_encode($xml);
?>

Output:
{"@attributes":{"semester":"1"},"student":[{"@attributes":{"position":"1"},"name":"John Doe","age":"18","marks":{"maths":"85","physics":"80","chemistry":"76"}},{"@attributes":{"position":"2"},"name":"Mary Johnson","age":"17","marks":{"maths":"78","physics":"56","chemistry":"87"}}]}

In the case where there are multiple nodes with the same name, the resulting JSON string will have one key pointing to an array of elements.

Example

<?php
$student = <<<XML
<student>
    <name>John Doe</name>
    <age>18</age>
    <units>
        <unit>Discrete Structures</unit>
        <unit>Electrical Principles</unit>
        <unit>Linear Algebra</unit>
        <unit>Probability and statistics</unit>
    </units>
</student>
XML;

$xml = simplexml_load_string($student);
echo json_encode($xml);
?>

Output:
{"name":"John Doe","age":"18","units":{"unit":["Discrete Structures","Electrical Principles","Linear Algebra","Probability and statistics"]}}

Conclusion

Converting XML to JSON using PHP is a straightforward process. By following the steps outlined in this guide, you can confidently handle XML data and seamlessly integrate it into JSON-based environments.