How To Parse XML With PHP: Loop Through Results

Now that you know how to parse XML with attributes am ready to show you how to loop through all the entries in an XML file. We will be using the same XML file we’ve been using since part 1 and 2, but am going to add another book entry, so our XML file “xml-file.xml” will now look like this.

<?xml version="1.0" encoding="UTF-8"?>
<library>
	<book isbn10="1234567890" isbn13="1234567890123">
		<title>PHP and MySQL</title>
		<author fname="miguel" lname="alvarez">Miguel Alvarez</author>
		<publisher>WebHole</publisher>
		<price>1.99</price>
	</book>
	<book isbn10="8957468873" isbn13="0984837828189">
		<title>JAVA 123</title>
		<author fname="william" lname="vega">WIlliam Vega</author>
		<publisher>WebHole</publisher>
		<price>2.99</price>
	</book>
	<book isbn10="897eux7648" isbn13="87dhnbe3w289">
		<title>Advanced C++</title>
		<author fname="miguel" lname="alvarez">Miguel Alvarez</author>
		<publisher>WebHole</publisher>
		<price>0.99</price>
	</book>
</library>

As you can see all that changed was that I added another book. Now let’s talk about ther “parser.php” file.

Let’s begin by setting up the basics ( a url and the function )

<?php
$url = "xml->file.xml";
$xml = simplexml_load_file($url);

This time we are going to use a function called “foreach” this function is basically a neat way to loop through arrays, you could accomplish the same things usin a “while” or a “for” loop, but this function is not hard to get if you haven’t seen it, so we are going to stick with it.

If you’ve never heard of  or used “foreach” here is a quick intro. “foreach” takes an array you want to loop through and a variable you want to use to represent that array. In our case, the array is the <book> tag and we’ll use a variable called $book to represent each <book> tag. So here is  the set up of foreach.

<?php
$url = "xml->file.xml";
$xml = simplexml_load_file($url);

foreach($xml->book as $book)
{
 // display data code goes here
}

Since “$xml->book” represents the the <book> tag, our code could be “translated” to  “for each <book> tag, loop through and represent each tag as $book”, I hope you understood the way I worded it ;).

Okay, so now let’s display some data. We’ll begin by showing the title of each book in our XML file.

<?php
$url = "xml->file.xml";
$xml = simplexml_load_file($url);

foreach($xml->book as $book)
{
echo "<p>";
echo "<strong>Title:</strong> ".$book->title."<br/>";
echo "</p>";
}

and after running the script you should see the following

Title: PHP and MySQL

Title: JAVA 123

Title: Advanced C++

Foreach begins at zero and increases by one automatically that’s also why I prefer this method.

With foreach we do not have to use something like $book[‘0’] or  $book[‘1’] like we we would with a “for” or “while” loop to tell the script which book we were talking about, we just have to tell it which tag or attribute we want to get.  Now let’s show some more data from each book.

<?php
// specify url of xml file
$url = "xml-file.xml";
// get xml file contents
$xml = simplexml_load_file($url);

// loop begins
foreach($xml->book as $book)
{
// begin new paragraph
echo "<p>";
// show isbn10
echo "<strong>ISBN10:</strong> ".$book["isbn10"]."<br/>";
// show isbn13
echo "<strong>ISBN13:</strong> ".$book["isbn13"]."<br/>";
// show title
echo "<strong>Title:</strong> ".$book->title."<br/>";
// show author
echo "<strong>Author:</strong> ".$book->author."<br/>";
// show publisher
echo "<strong>Plublisher:</strong> ".$book->publisher."<br/>";
// show price
echo "<strong>Price:</strong> ".$book->price."<br/>";
echo "</p>";
// end paragraph
}
// loop ends

?>

And the output for that should be this.

ISBN10: 1234567890
ISBN13: 1234567890123
Title: PHP and MySQL
Author: Miguel Alvarez
Plublisher: WebHole
Price: 1.99

ISBN10: 8957468873
ISBN13: 0984837828189
Title: JAVA 123
Author: WIlliam Vega
Plublisher: WebHole
Price: 2.99

ISBN10: 897eux7648
ISBN13: 87dhnbe3w289
Title: Advanced C++
Author: Miguel Alvarez
Plublisher: WebHole
Price: 0.99

We have successfully looped through all the books in the xml file, if you got any questions about this three part tutorial email me and/or leave a comment. Next time I’ll show you some cool content you can parse with PHP.