Digital Web Magazine

The web professional's online magazine of choice.

PHP and XML Sitting in a Tree

Got something to say?

Share your comments on this topic with other web professionals

In: Articles

By Kerri Hicks

Published on February 4, 2007

There are scores of ways to store, work with, and retrieve data on the web. Simple databases, relational databases, XML, even custom flat files can hold our product catalogs, user information, and other repositories of important stuff that we want to share with the world. There are some logical ways to work with different kinds of data—for example, many people who use MySQL databases for storage will write PHP scripts to hook into their data, because of PHP’s native support for MySQL. On the other hand, XML purists often use XSLT (which is itself also XML) to work with their marked-up files. As web developers, it often comes down to us to make the decisions about how to store new data, and even more importantly, how to work with data that our clients give to us.

XML continues to gain popularity as a data format on the web, while databases such as MySQL are consistently improving and have an ever-growing user base. Both XML and MySQL allow you to represent your data with key/value pairs—for example, say you want to express the fact that the title of your favorite album is I Like Cows. The XML expression <title>I Like Cows</title> can be used the same way as the MySQL command INSERT into tablename (title) VALUES ('I Like Cows') ; . Both constructs allow you to store as data the fact that I Like Cows is a title.

XML offers you a document tree to help lend structure to your data, XSLT to munge the data into submission, and parsers for output. Most MySQL users, on the other hand, will use the power of functions in PHP or other scripting languages to articulate relationships between tables, fields, and values.

There is often no right way to store and serve your data, and practically speaking, the best route to choose is often the one you know best. However, PHP5 has brought with it a new function, SimpleXML, which can help offer you even more options, and is a powerful addition to your scripting toolbox.

Why Choose?

A few years ago, I needed to re-use some existing XML documents and some MySQL data in the same PHP script. After several late nights of poking at XML extensions and writing really ugly regular expressions, I was able to get it working, but it was an inelegant solution and, frankly, a huge pain.

Had SimpleXML been available back then, it would have made that project a lot easier. Using SimpleXML in PHP allows you to grab a chunk of XML and convert it into a PHP object, which is then broken down further, turning the XML elements into object properties which PHP can then act on. Multiple XML elements of the same type are represented as an array, which you can loop over with foreach or while until the sun goes down.

Now that PHP is showing XML some real love, it makes perfect sense to use the two together when the time is right.

The Basics

I’ll illustrate the basic concept using a simple grocery-shopping list containing XML that you can create yourself. (What, you don’t write your shopping lists in XML?) Remember, with XML, you can make up your own element names, and they’ll work just fine, as long as you use them consistently and nest them properly.

(N.B. This example is meant to be illustrative, and is likely not the way you’d actually want to write your XML grocery list for production purposes.)

<?xml version="1.0"?> 
  <groceryList> 
      <listName>My Shopping List</listName>
      <foodGroup>
          <groupName>Fruits and Vegetables</groupName>
          <item>
              <name>Pomegranate</name>
              <howMuch>2</howMuch>
          </item>
          <item>
              <name>Carrots</name>
              <howMuch>1 pound</howMuch>
          </item>
      </foodGroup>
      <foodGroup>
          <groupName>Grains</groupName>
          <item>
              <name>Couscous</name>
              <howMuch>6 ounces</howMuch>
          </item>
      </foodGroup> 
      <foodGroup>
          <groupName>Candy</groupName>
          <item>
              <name>Crunchie Bar</name>
              <howMuch>1 gross</howMuch>
          </item>
      </foodGroup> 
  </groceryList>

In plain English, we’ve got three food groups, Fruits and Vegetables, Grains, and Candy. In addition to a name, each of the groups also contains one or more items, and each item contains a name and a quantity. Pretty simple. (If you’d like to follow along hands-on, copy that XML into your favorite text editor and save it to your web server as groceries.xml1).

Now we’ll write the PHP that will allow us to grab that XML data and use it in a script.

(N.B. As with the XML above, this PHP is for illustrative purposes. Real-world use would beg for a great deal of optimization, and an XHTML wrapper.)

<?php 
  // set the XML file name as a PHP string
  $myGroceryList = "groceries.xml" ; 
  // load the XML file 
  $xml = @simplexml_load_file($myGroceryList) or die ("no file loaded") ; 
  // assign the listName element to a string
  $nameOfMyShoppingList = $xml->listName ;
  echo "<h1>List: " . $nameOfMyShoppingList . "</h1>";
?>

returns List: My Shopping List

(Try it yourself. Copy the code above and save it to a text file named list.php. Make sure it’s in the same directory as groceries.xml. Then load list.php in your browser, and see what you get.)

It doesn’t get much more basic than that. The script will read the XML file, load the whole thing into an object called $xml, and then walk through the XML to find an element called listName. It’ll assign the value of the XML element listName (in our case, “My Shopping List”) to the variable $nameOfMyShoppingList, and then I can use that variable anywhere PHP will allow it.

Using your data

Now that your XML file is loaded into an object, and you’ve learned how to display simple values, you can really have your way with it. Let’s create a loop that will take each foodGroup element, display its name, and then list each of the items’ names and quantities. (If you’re still following along, add the following code to the end of list.php, just before the ?>.)

foreach ($xml->foodGroup as $foodGroup) {
  echo "<h2>Food group name is " . $foodGroup->groupName . "</h2>" ;
  foreach ($foodGroup->item as $foodItem) {
      echo "<br /> Item: " . $foodItem->name ;
      echo "<br /> Quantity: " . $foodItem->howMuch ;
      echo "<br />" ;
  }
  echo "<br />" ;
}

will give you:

List: My Shopping List
Food group name is Fruits and Vegetables
Item: Pomegranate
Quantity: 2
Item: Carrots
Quantity: 1 pound
Food group name is Grains
Item: Couscous
Quantity: 6 ounces
Food group name is Candy
Item: Crunchie Bar
Quantity: 1 gross  

(See the results in action.)

If you’re already an expert with XML queries, you’ll be happy to know that SimpleXML also supports XPath.

Other Applications

Since XHTML is a flavor of XML, you can use SimpleXML to parse data from any web page that is well-formed XHTML. I can’t stress enough how cool it is to be able to maintain a master file of simple data in XHTML, but then be able to move, change, and redisplay that data with some simple PHP, while you also make it available to other people to use for their own projects. To accomplish this with a well-formed XHTML file anywhere on the web, simply use a URI instead of a file name as the value in simplexml_load_file, like this:

// set the XML file's URI as a PHP string
      $bobsGroceryList = "http://www.bobgroceries.com/list.xml" ; 
  // load the XML file 
      $xml = @simplexml_load_file($bobsGroceryList) or die ("no file loaded") ; 

This comes in very handy when you want to use data from services on the web that serve up XML in response to queries. One fun example is ISBNdb, a book data service. Their really simple API allows you to get basic data about books, and will deliver it to you (or your PHP script) in XML format. Take a look at this example—but remember to come back here and finish reading! (If you are interested in seeing or using the CC-licensed code for this ISBN example, just drop me an email and I’ll send it along to you.)

Go Forth, Be Fruitful, and Iterate

Now that you’ve got an understanding of the power of SimpleXML, you may want to consider the new ways you can store and work with data for your projects. Productive work often requires the power and versatility of a database, but for some applications, SimpleXML is a terrific solution.

Footnote

1 Make sure your web server is running PHP5—SimpleXML is active by default in PHP5, so this should work unless your sysadmin intentionally turned it off (additional details).

Got something to say?

Share your comments  with other professionals (14 comments)

Related Topics: PHP, XML, Programming

 

Kerri Hicks is a programmer in Brown University’s Scholarly Technology Group, and is the copy chief at Digital Web. She’s been a salaried web developer since 1994, but has yet to ask anyone to actually get off of her lawn. She has been known to decorate her clothing with likenesses of robots.

Media Temple

via Ad Packs