How to Parse XML Data in PHP
Page 1 of 1
How to Parse XML Data in PHP
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:
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.
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.
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.
$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
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:
- An XML file saved on your local machine or server or an XML URL where XML File is there.
- 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.xmlSuppose 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.
Next node is
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
Similar topics
» How to Retrieve Data From Internet in Python
» Hackers can use python's pip to steal your data
» Pen-testing Guide PCI Data Security Standard PCI DSS
» How easy is it to capture data on public free Wi-Fi
» Store Data Inside Other Files On windows
» Hackers can use python's pip to steal your data
» Pen-testing Guide PCI Data Security Standard PCI DSS
» How easy is it to capture data on public free Wi-Fi
» Store Data Inside Other Files On windows
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|