SimpleXML is a PHP extension introduced with PHP 5. It allows users to easily handle XML data in PHP. SimpleXML converts any XML data to an object which can be easily processed with normal property selectors and array iterators.
You must have installed php-simplexml extension on your system to use examples of this tutorial.
A Sample XML File
Here is a sample XML file used for this tutorial. The XML filename is employees.xml which you will see in further examples of this tutorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0"?> <company> <employee> <firstname>Tom</firstname> <lastname>Cruise</lastname> <designation>MD</designation> <salary>500000</salary> </employee> <employee> <firstname>Tyler</firstname> <lastname>Horne</lastname> <designation>CEO</designation> <salary>250000</salary> </employee> </company> |
Read Specific XML Elements
Use simplexml_load_file function to load external XML file in your PHP program and create an object. After that, you can access any element from the XML by this object as follows.
1 2 3 4 5 | <?php $xmldata = simplexml_load_file("employees.xml") or die("Failed to load"); echo $xmldata->employee[0]->firstname . "<\n>"; echo $xmldata->employee[1]->firstname; ?> |
Output:
Tom Tyler
If the XML file is available on the remote server, you can use HTTP URL for the XML file as followings:
1 2 3 4 5 | <?php $xmldata = simplexml_load_file("https://tecadmin.net/employees.xml") or die("Failed to load"); echo $xmldata->employee[0]->firstname . "<\n>"; echo $xmldata->employee[1]->firstname; ?> |
Read XML Elements In A Loop
In this example, we use the foreach method to iterate through the entire XML file and read elements from XML. Foreach loop access all children of an object.
1 2 3 4 5 6 7 8 9 | <?php $xmldata = simplexml_load_file("employees.xml") or die("Failed to load"); foreach($xmldata->children() as $empl) { echo $empl->firstname . ", "; echo $empl->lastname . ", "; echo $empl->designation . ", "; echo $empl->salary . "<\n>"; } ?> |
Output:
Tom, Cruise, MD, 500000 Tyler, Horne, CEO, 250000
13 Comments
how to read xml from Report Windows Log ?
like this:
Please help
how to read xml from Report Windows Log ?
like this:
4663
……..
Please help
Thanks for share it Rahul.
hey,
I store xml data in MySQL database. Now, I want to retrieve all the that data with PHP and show in a website.
But it returns nothing
Hello,
How to retrieve the data if employees.xml is from another website?
You can simply put http url. I have also added an example in this tutorial.
Hi, Google article, i stay practice php and i so like this language. Thanks 🙂
Thanks for this post, helped me thanks brow!!
if $xmldata containes a single quote how can that be handled ?
I want to use something like str_replace(“‘”,” “,$xmldata) but that wont work
Hi thank you for solution, for nested xml?
Example:
–
63856
–
–
NWBASO4ML-0008
–
NWBASO4ML-0005
Base Solution N 4 ml in acqua
Hey my xml markup lost.
There is an error on your sample code. You forget the s on implexml_load_file in your loop example. Please correct it.
Thanks Taylor, I have corrected the typo.