How To Read XML Data to JSON in PHP Tutorial

Share this Article
Reading Time: 4 minutes
160 Views

Inside this article we will see the concept i.e How to read xml data to json in PHP. Article contains classified information. It will give the complete idea of xml file reading in PHP.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you learn reading xml file here, you can use the same concept in data seeding to database via xml file. This is step by step tutorial in PHP about read xml data to json data.

How to convert a XML data into a JSON data, we will see the whole concept here in a very easy way.

Learn More –

Let’s get started.


Create an Application

Create a folder php-xml in your localhost directory.

Case #1

In this case we assume that XML data is written into a separate file.

Inside this folder, create a file called index.php.

Suppose we have a XML file as students.xml

<?xml version='1.0' standalone='yes'?>
<students>
    <student>
      <rollNo>2</rollNo>
      <name>Sanjay Kumar</name>
      <gender>Male</gender>
      <subjects>
         <subject>English</subject>
         <subject>Hindi</subject>
         <subject>Physics</subject>
      </subjects>
    </student>
    <student>
      <rollNo>5</rollNo>
      <name>Vijay Kumar</name>
      <gender>Male</gender>
      <subjects>
         <subject>Hindi</subject>
         <subject>Physics</subject>
      </subjects>
    </student>
    <student>
      <rollNo>6</rollNo>
      <name>Ashish Kumar</name>
      <gender>Male</gender>
      <subjects>
         <subject>English</subject>
         <subject>Hindi</subject>
      </subjects>
    </student>
    <student>
      <rollNo>10</rollNo>
      <name>Dhananjay Negi</name>
      <gender>Male</gender>
      <subjects>
         <subject>English</subject>
         <subject>Hindi</subject>
         <subject>Physics</subject>
      </subjects>
    </student>
</students>

Open index.php and write this complete code into it.

<?php   

$xml = simplexml_load_string(file_get_contents("students.xml"));
// convert the XML string to JSON
$jsonData = json_encode($xml, JSON_PRETTY_PRINT); 

echo '<pre>';
print_r($jsonData);

?>

Case #2

This is the case where we assume XML data into php file.

<?php   
$xml_string =  <<<XML
<?xml version='1.0' standalone='yes'?>
<students>
    <student>
      <rollNo>2</rollNo>
      <name>Sanjay Kumar</name>
      <gender>Male</gender>
      <subjects>
         <subject>English</subject>
         <subject>Hindi</subject>
         <subject>Physics</subject>
      </subjects>
    </student>
    <student>
      <rollNo>5</rollNo>
      <name>Vijay Kumar</name>
      <gender>Male</gender>
      <subjects>
         <subject>Hindi</subject>
         <subject>Physics</subject>
      </subjects>
    </student>
    <student>
      <rollNo>6</rollNo>
      <name>Ashish Kumar</name>
      <gender>Male</gender>
      <subjects>
         <subject>English</subject>
         <subject>Hindi</subject>
      </subjects>
    </student>
    <student>
      <rollNo>10</rollNo>
      <name>Dhananjay Negi</name>
      <gender>Male</gender>
      <subjects>
         <subject>English</subject>
         <subject>Hindi</subject>
         <subject>Physics</subject>
      </subjects>
    </student>
</students>
XML;

$xml = simplexml_load_string($xml_string);
// convert the XML string to JSON
$jsonData = json_encode($xml, JSON_PRETTY_PRINT); 

echo '<pre>';
print_r($jsonData);

?>

Application Testing

Open browser and hit this URL: http://localhost/php-xml

We hope this article helped you to learn How To Read XML Data to JSON in PHP Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4