How to Convert XML data into JSON using PHP ? - GeeksforGeeks

13 Apr, 2021
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:
Used Methods:
simplexml_load_string() Method: This function is used to convert the XML string into an object.
json_encode() Method: This function is used to encode a value to JSON format.
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_encoe function$jsondata = json_encode($xmldata);   // Display json dataprint_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" 
        }}
    ]
}
JSON
PHP-function
PHP-Questions
PHP-XML
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
PHP in_array() Function
How to convert array to string in PHP ?
How to delete an array element based on key in PHP?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?