Friday, October 10, 2025
HomeLanguagesHow to Convert XML data into JSON using PHP ?

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:

  • 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" 
        }}
    ]
}
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS