Open In App

How to Convert XML data into JSON using PHP ?

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:

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:

PHP code: The following is the content for the file “base.php” file.




<?php
 
// student details xml data taken as an String
$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>';
  
// Load xml data into xml data object
$xmldata = simplexml_load_string($xml);
 
// Encode this xml data into json
// using json_encode function
$jsondata = json_encode($xmldata);
  
// Display json data
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" 
        }}
    ]
}

Article Tags :