You can build really cool apps with API’s and most sites will send either JSON or XML back to your app, I prefer JSON actually but some of these API’s will only send XML and, while you can always convert XML to JSON, it’s always better to save that processing time.
The Function: simplexml_load_file
Since version 5, PHP comes with this easy to use function called “simplexml_load_file” , the function requires only one parameter, the URL, but has other optional ones. The URL can be relative for files in the same server where the script is running, or absolute for external.
The XML File (data) “xml-file.xml”
Let’s begin by creating an XML file called “xml-file.xml” with the following contents.
<?xml version="1.0" encoding="UTF-8"?> <library> <book> <title>PHP and MySQL</title> <author>Miguel Alvarez</author> <publisher>WebHole</publisher> <price>1.99</price> </book> <book> <title>JAVA 123</title> <author>WIlliam Vega</author> <publisher>WebHole</publisher> <price>2.99</price> </book> </library>
As might guess this file could come from a library’s database ( those books are fake by the way ). So as you can see we have two books in this xml library file and inside each “<book>” tag there is information about each particular book.
The PHP File (parser) “parser.php”
Now make a file and call it “parser.php”, make sure that both file are in the same folder for the purposes of this tutorial.
Let’s begin this script by create a varible with the path and file name of the XML file you want to parse
<?php $url = 'xml-file.xml';
This is where the fun begins. Create a variable $xml. We will then use simplexml_load_file($url); to load the file into the variable.
<?php $url = 'xml-file.xml'; $xml = simplexml_load_file($url);
Now $xml has the contents of “xml-file.xml” loaded into an array-like structure that it’s easy to learn if you know arrays.
Think of the variable $xml as if it was the <library> tag in our xml file.
Let’s us then show the title of the first book in our xml file, that is, the first <book> tag from the top.
// get first book title $title=$xml->book->title; // show title echo $title;
That was easy right? You can see that, like in arrays, to get the first book you actually begin with a zero, then, to get the title of the second book you would simply replace that zero with a one, like this.
// get second book title $title=$xml->book->title; // show title echo $title;
Do I have to tell you that to get the price of the first book you would use $title=$xml->book->price; like this?
// get price of first book $price=$xml->book->price; // show price echo $price;
You actually don’t need to use “” if you know you are referring to the first. So the following two lines do the same thing.
Okay so now let’s take a look at the whole php file.
<?php $url = 'xml-file.xml'; $xml = simplexml_load_file($url); // get first book title $title=$xml->book->title; // show title echo $title; echo '<br/>'; // get second book title $title=$xml->book->title; // show title echo $title; echo '<br/>'; // get first book title again $title=$xml->book->title; // display title echo $title; ?>
You have probably, and if not you will, encounter XML with attributes and you’ll want to read those as well, you are also probably wondering how you could loop through 100 books, I don’t know about you but I like to read and write short tutorials so am going to let you play with what you learned today and talk about these two topics on part 2, c u later.