How to Parse XML Data in PHP

By Saruque Ahamed Mollick

Hi learners, today I am going to write how to parse XML data in PHP with examples. This tutorial will help you to understand the XML data structure and parsing data from each node of your XML file.

Requirements to parse an XML file:

  1. An XML file saved on your local machine or server or an XML URL where XML File is there.
  2. PHP Program to parse the data from that XML file.

I am gonna first create an easy XML file so that you can understand How easily XML data parsing in PHP can be done. Thereafter we will understand some intermediate level data parsing from an XML file Using PHP.

After reading this tutorial You might be interested in Find Out What Happened On This Day PHP Script Using Hiztory API This is nothing but Simple XML Parsing to show what happened on this day.



You can watch this video below of this tutorial on How to parse XML in PHP

Subscribe our YouTube Channel to get updated with latest tutorial like this.

 

 

Parse XML Data In PHP

We need to have an XML data file first. To make you understand easily I am gonna create an XML file having some parent node and child node. So just copy the below XML Code and save it as an XML file. For example, I will save it as data.xml

<tutorial>
  <topic>
    <name>Java</name>
    <level>Intermediate</level>
    <duration>127 Hours</duration>
  </topic>
  <topic>
    <name>Python</name>
    <level>Advanced</level>
    <duration>157 Hours</duration>
  </topic>
  <topic>
    <name>PHP</name>
    <level>Advanced</level>
    <duration>128 Hours</duration>
  </topic>
</tutorial>

Suppose we have to get the topic name Java from the XML file. Then how can we get this using PHP?

Just look at the XML structure. <tutorial> is our parent node and every XML file must have a Parent node. So we don’t need to bother about this node.
Next node is <topic> node. Under this node, we have content like name, level, duration.

To display Java from the XML file we will use the below PHP program to parse XML data.

<?php
   
   $xml = simplexml_load_file('data.xml');
   $data= $xml->topic->name;
   echo $data;
   
   
   ?>


  

Explanation of the above code:

simplexml_load_file('data.xml')

this function Convert an XML file into a SimpleXMLElement object. We assign $xml variable to that object.

$xml->topic->name

This line means that we are accessing the data of the first node topic. Under topic, there are many tags like name, level, duration. But we want to access only the name of the topic so we have used name here.

So, If you run this program you will get the output: Java.

Parse XML Data By using the node index in PHP

But here the question arises. Why only the first topic name is showing though we have three topic nodes under the tutorial node.

Because by default it will show the first element of the array list. The data are stored here in an array. And if we don’t mention the index it will take  0 index by default.

Find Out What Happened On This Day PHP Script Using Hiztory API

We have three nodes under tutorial with the same name topic. So if we want to call a particular element we need to use the index.

Now suppose we need to display the 3rd topic name from the XML data.

Our program will be like this.

<?php
   
   $xml = simplexml_load_file('data.xml');
   $data= $xml->topic[2]->name;
   echo $data;
   
   
   ?>


  

This program will display PHP.

we used topic[2] here because this is like array indexing and index always starts with zero.

Till this, we were in a beginner lesson.
Now go to the intermediate level. Suppose You have to display all the names of the topic. And you have a very huge number of topics in the XML file. You don’t even know the number of topics listed in the XML file.

Parsing All Particular Nodes from an XML file using PHP

<?php
   
   $xml = simplexml_load_file('data.xml');
   $data= $xml->topic;
   foreach ($data as $showname) {
   	foreach ($showname->name as $names) {
   		echo "$names"."<br>";
   		
   	}
   	
   }
   
   
   ?>


  

Output:

Java
Python
PHP

Using foreach loop we can achieve this goal.

Leave a Reply

Your email address will not be published. Required fields are marked *