Open In App

How to Convert XML data into JSON using PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to convert XML data into JSON format using PHP.

Requirements:

  • XAMPP Server

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:

  • Start XAMPP server

  • 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
 
// 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" 
        }}
    ]
}


Last Updated : 29 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads