Tutorial – Calling XML through PHP

This tutorial is actually based on another tutorial I stumbled upon recently. I’ve decided to write my own version simply because well it was written using HTML and tables. I will be changing it to support div tags and XHTML formatting.

This tutorial will be using 2 files the library.XML which will be what we pull the data from and index.php which will pull the data from the library.

As I have in the past I will be posting the complete code and then break it down piece by piece.

Here is what the library.XML will look like:

<?XML version="1.0"?>
<library>
	<book>
		<title>Hannibal</title>
		<author>Thomas Harris</author>
		<genre>Suspense</genre>
		<pages>564</pages>
		<price>8.99</price>
		<rating>4</rating>
	</book>
	<book>
		<title>Run</title>
		<author>Douglas E. Winter</author>
		<genre>Thriller</genre>
		<pages>390</pages>
		<price>7.49</price>
		<rating>5</rating>
	</book>
	<book>
		<title>The Lord Of The Rings</title>
		<author>J. R. R. Tolkien</author>
		<genre>Fantasy</genre>
		<pages>3489</pages>
		<price>10.99</price>
		<rating>5</rating>
	</book>
	<book>
		<title>Heck Designs</title>
		<author>Robert Heck</author>
		<genre>Art</genre>
		<pages>3489</pages>
		<price>10.99</price>
		<rating>0</rating>
	</book>
</library>

Now for simplicity’s sake lets condense this and explain it a bit further.

<?XML version="1.0"?>
<library>
	<book>
		<title>Hannibal</title>
		<author>Thomas Harris</author>
		<genre>Suspense</genre>
		<pages>564</pages>
		<price>8.99</price>
		<rating>4</rating>
	</book>
</library>

The first line is simply declaring what version of XML is being used. Now below that you’ll notice that there are opening tags with their respective closing tags much like HTML/XHTML but the biggest difference is that you’ll see “library”, “book”, etc. These aren’t anything that exists as a predefined tag but instead are defined because I want them to be defined. That’s right you define the name of a tag in XML.

Inside of the book tag you’ll notice that each tag inside of it has something words inside of it, such as the title tag has the title of a book that is in the library.

Now lets take a look at the PHP/XHTML that will interpret this information and display it on the website. So lets take a look at the code that will go into the index.php:





The Library



The Library

Title
Author
Price
User Rating
'; break; case "TITLE": echo '
'; break; case "AUTHOR": echo '
'; break; case "PRICE": echo '
'; break; case "RATING": echo '
'; break; default: break; } } ?> "; break; case "TITLE": echo "
"; break; case "AUTHOR": echo "
"; break; case "PRICE": echo "
"; break; case "RATING": echo "

"; break; default: break; } // clear current tag variable $currentTag = ""; } ?> "Words fail me!", 1 => "Terrible", 2 => "Bad", 3 => "Indifferent", 4 => "Good", 5 => "Excellent" ); // format the data switch ($currentTag) { case "TITLE": echo "$data"; break; case "AUTHOR": echo $data; break; case "PRICE": // add currency symbol for price echo "$" . $data; break; case "RATING": // get text rating echo $ratings[$data]; break; default: break; } } ?>

You’ll notice a few things I’ve done that I haven’t done in the past which is include the header section with doc type and meta tags as well as some CSS. I’ve also broken up the PHP into sections with opening and closing PHP tags this is simply to make it easier to see where the code breaks up into sections. You don’t need to have a full blown header but you do need the CSS, since I’m using div tags instead of tables you need some CSS to help style the divs which is not a requirement with tables.

The Library

Title
Author
Price
User Rating

You could actually add this into the library.XML and have it be the first set called but I built it into XHTML because these are the titles and not actually part of the library. You’ll notice something about the div, it has 2 classes being attached to it. Just in case you didn’t know in the quoted section where the name of the class is defined if you add a space you can assign multiple classes to 1 div. This also applies to IDs as well.

Here is the first section of the PHP code and what this does is define the which file will be used (library.XML), starts the XML parser, defines the callback functions, opens the XML file, and reads it. There are also error checks in place as well as a clean up, there are notes in the code which I left so you can see which section does what.

';
			break;
			case "TITLE":
				echo '
'; break; case "AUTHOR": echo '
'; break; case "PRICE": echo '
'; break; case "RATING": echo '
'; break; default: break; } } ?>

This is creating the opening tags based on the information in the XML. It searches for a matching defined tag, once it finds one it creates an opening div and then stops. It continues on looking for the next tag in the XML and repeats the process. This is repeated until reaching the end of the cases in the PHP. Notice that we don’t have the pages or genre tags being checked.

";
			break;
			case "TITLE":
				echo "
"; break; case "AUTHOR": echo "
"; break; case "PRICE": echo "