PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How to Parse XML Data in PHP

Go down

How to Parse XML Data in PHP Empty How to Parse XML Data in PHP

Post by jamied_uk 7th February 2022, 21:52

How to Parse XML Data in PHP
How to Parse XML Data in PHP E58a1f9ea81abbcbeb6ad0e8a257ea84?s=96&d=mm&r=g 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.
 
How to Parse XML Data in PHP Hqdefault
 

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


Java
Intermediate
127 Hours


Python
Advanced
157 Hours


PHP
Advanced
128 Hours


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.  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  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:
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.

$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"."
";

}

}


?>
Output:
Java
Python
PHP
Using foreach loop we can itterate the results!

Thanks To
codespeedy.com/how-to-parse-xml-data-in-php
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum