How to Convert JSON Data to XML in PHP
John Mwaniki / Updated on 07 Jul 2024As a developer, you are likely to find yourself needing to convert JSON data to XML, perhaps due to specific system requirements or interoperability needs.
In this article, we will cover how to convert JSON data to XML format using PHP, providing clear and practical examples.
Converting JSON Data to XML in PHP
Before getting into the conversion process, let's briefly recap the characteristics of JSON and XML.
Below is JSON data with a person's information:
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
Here is the same data represented in the XML format:
<person>
<name>John Doe</name>
<age>30</age>
<city>New York</city>
</person>
PHP provides several built-in ways of converting data from JSON to XML in a relatively easy process.
In this article, we will use the SimpleXML extension and the json_decode()
function.
The json_decode()
function is used for parsing JSON and SimpleXML extension for creating XML.
Example
<?php
// Sample JSON data
$jsonString = '{
"name": "John Doe",
"age": 30,
"city": "New York"
}';
// Decoding JSON data
$decodedData = json_decode($jsonString);
// Creating a SimpleXML object
$xml = new SimpleXMLElement('<person></person>');
/*
* Adding array elements from decoded JSON data as
* child elements to SimpleXML object
*/
foreach ($decodedData as $key => $value) {
$xml->addChild($key, htmlspecialchars($value));
}
// Creating an XML string from the SimpleXML object
Header('Content-type: text/xml');
echo $xml->asXML();
?>
This code outputs:
<person>
<name>John Doe</name>
<age>30</age>
<city>New York</city>
</person>
In this example, we had our data as a JSON string initially. We decoded it using the json_decode() function. We then created a SimpleXML object with the <person>
tags as its root element. Then we iterated through the array elements from the decoded JSON data, adding them to the SimpleXML object as child elements. Lastly, we used the asXML()
function gets a well-formed XML string from the SimpleXML object.
The Header('Content-type: text/xml')
is just for enabling us to display the resulting XML with tags on the page with the echo statement.
Handling Nested Structures
JSON strings often contain nested structures, which need special attention during conversion.
Example
<?php
// Sample JSON data
$student = '{
"name": "John Doe",
"age": "17",
"city": "Nairobi",
"marks": {
"maths": "80",
"physics": "75",
"chemistry": "72"
}
}';
// Decode JSON
$data = json_decode($student);
// Creating SimpleXML object
$xml = new SimpleXMLElement("<student></student>");
/*
* Adding array elements from decoded JSON data as
* child elements to SimpleXML object via a recursive function
*/
function arrayToXml($data, &$xml) {
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value)) {
$subElement = $xml->addChild($key);
arrayToXml($value, $subElement);
}
else {
$xml->addChild($key, $value);
}
}
}
// Passing decoded data and SimpleXML object to recursive function
arrayToXml($data, $xml);
// Outputing XML
Header('Content-type: text/xml');
echo $xml->asXML();
?>
This code outputs:
<student>
<name>John Doe</name>
<age>17</age>
<city>Nairobi</city>
<marks>
<maths>80</maths>
<physics>75</physics>
<chemistry>72</chemistry>
</marks>
</student>
In this example, we had a nested JSON string, where the "marks" key comprised an object with three key-value pairs as its value.
We created a custom recursive function arrayToXml()
that iterates through the data, creating nested XML elements as needed.
That's it!
Now you can comfortably convert JSON data to XML in PHP.