How to Convert XML data into JSON using PHP ?
Last Updated :
29 Aug, 2022
In this article, we are going to see how to convert XML data into JSON format using PHP.
Requirements:
Introduction: PHP stands for hypertext preprocessor, which is used to create dynamic web pages. It also parses the XML and JSON data. XML stands for an extensible markup language in which we can define our own data.
Structure of XML:
<root>
<child>
<subchild> ... </subchild>
</child>
</root>
Example: We are considering student XML data and converting it into JSON format.
<student>
<details>
<address>
<firstname>sravan kumar</firstname>
<city>kakumanu</city>
<zip>522112</zip>
</address>
</details>
<details>
<address>
<firstname>sudheer</firstname>
<city>guntur</city>
<zip>522112</zip>
</address>
</details>
<details>
<address>
<firstname>radha kumar</firstname>
<city>ponnur</city>
<zip>456345</zip>
</address>
</details>
<details>
<address>
<firstname>vani</firstname>
<city>noida</city>
<zip>456644</zip>
</address>
</details>
</student>
JSON stands for JavaScript Object notation which is in the format of array-like structure.
Structure of JSON:
{
"data1": "value1",
"data2": "value2",
"datan": "valuen"
}
Example:
{"details":
[{
"address": {
"firstname": "sravan kumar",
"city": "kakumanu",
"zip": "522112"
}
},
{
"address": {
"firstname": "sudheer",
"city": "guntur",
"zip": "522112"
}
},
{
"address": {
"firstname": "radha kumar",
"city": "ponnur",
"zip": "456345"
}
},
{
"address": {
"firstname": "vani",
"city": "noida",
"zip": "456644"
}
}]}
Similarities of JSON and XML:
- Both JSON and XML are self-describing.
- JSON and XML are hierarchical.
- JSON and XML can be parsed which are used in many programming languages.
Differences between JSON and XML:
JSON |
XML |
JSON doesn’t use an end tag |
XML uses end tag |
JSON is shorter than XML |
XML is longer than JSON |
JSON is quicker to read and write |
XML is a bit slower than JSON |
Arrays can be used by JSON |
XML can not use arrays. |
Used Methods:
Steps:
- Open notepad and type the following code and save it as base.php in xampp-htdocs folder.
PHP code: The following is the content for the file “base.php” file.
PHP
<?php
$xml = '<?xml version= "1.0" encoding= "utf-8" ?>
<student>
<details>
<address>
<firstname>sravan kumar</firstname>
<city>kakumanu</city>
<zip>522112</zip>
</address>
</details>
<details>
<address>
<firstname>sudheer</firstname>
<city>guntur</city>
<zip>522112</zip>
</address>
</details>
<details>
<address>
<firstname>radha kumar</firstname>
<city>ponnur</city>
<zip>456345</zip>
</address>
</details>
<details>
<address>
<firstname>vani</firstname>
<city>noida</city>
<zip>456644</zip>
</address>
</details>
</student>';
$xmldata = simplexml_load_string( $xml );
$jsondata = json_encode( $xmldata );
print_r( $jsondata );
?>
|
Output: Type localhost/base.php in your browser.
{
"details": [
{
"address": {
"firstname": "sravan kumar",
"city": "kakumanu",
"zip": "522112"
}},
{
"address": {
"firstname": "sudheer",
"city": "guntur",
"zip": "522112"
}},
{ "address": {
"firstname": "radha kumar",
"city": "ponnur",
"zip": "456345"
}},
{ "address": {
"firstname": "vani",
"city": "noida",
"zip": "456644"
}}
]
}