How to Parse XML Data in PHP

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

    <duration>127 Hours</duration>
    <duration>157 Hours</duration>
    <duration>128 Hours</duration>

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.

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


Explanation of the above code:


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


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.

   $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

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




Using foreach loop we can achieve this goal.

5 responses to “How to Parse XML Data in PHP”

  1. Muhammad Asad says:

    I have very large file, and i tried your method it did not worked.

  2. Vince says:

    The description is very helpful.

    How would you load this to html table when submit button is clicked in form after selecting checkbox?

  3. Naveen says:

    Dear Saruque Ahamed Mollick, the video is really very good. could you please help in how to read a password protected xml file.


  4. Moussa says:

    Hello I would like to know how to generate PHP classes from XSD?
    can you help me?

    Thanks sir for such a great tutorial.

Leave a Reply

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