This series of articles will focus on XML, its applications in modern day web development and how PHP fits into this niche. In this article, we will focus specifically on the tools provided to us by PHP which enable us to manipulate XML data sources.

What is XML and Why Use it?

XML (eXtensible Markup Language) is a W3C standard designed to allow the easy exchange, storage and use of data between web applications and services.

Data encoded using the XML standard has meaning and structure which can be interpreted easily by humans and computers. XML data is platform and application independent. Need I say more? This in itself makes XML an ideal data exchange format for the internet (it was in fact developed for this very purpose). The recent increase in speed of broadband connections and the ever growing need by the consumer for feature-rich applications able to communicate with each other and share data across any medium means that XML web services and applications are becoming more and more abundant.

XML was created to address the problem of describing richly structured data on the web, which had thus far only been addressed loosely through the clever use of HTML.

Below is an example of a an XML document:
<?xml version="1.0"?>
<party>
    <location>My House</location>
    <time>7pm</time>
    <guest>
      <name>John Bloggs</name>
      <item>Crate of Fosters</item>
    </guest>
    <guest>
      <name>Sara Bloggs</name>
      <item>Umbrella</item>
    </guest>
    <guest>
      <name>David Fig</name>
      <item>Bombay Mix</item>
    </guest>
</party>
If you have not seen XML before, you may be thinking that it looks a lot like HTML. HTML is an application of SGML, a standard which XML is a subset of. The similarities end with the familiar looking tag delimiters however.

Just looking at the XML fragment above, we can see that the data is describing a party with some guests, each of which is bringing an item. The names of the tags used to describe the data are entirely the choice of the author; all the XML standard requires is that the data be consistent and the tags used to describe it be well formed. We can further enforce data integrity with a document type declaration (DTD) or an XML schema. For reasons of simplicity however we will work with plain, ordinary XML in this tutorial.

Applications of XML

We have already seen how XML can be used to describe any kind of data. XML is already in use today in many web applications, a few of which are described below:
The above is only a minute sample of the possible uses of XML. In future articles we will be looking into how we can use some of these applications in PHP.

Using XML in PHP

Since PHP 5, the options available to us with which PHP can interact with XML have broadened significantly. The best that the latest version of PHP 4 was able to offer was the unstable non w3c compliant DOM XML extension.
I will be focusing on three of the methods provided to us in PHP 5 which allow us to interact with XML: DOM, Simple XML and XPath. Where possible I will suggest a situation and data which is best suited to the method in question. All sample code will use a simple XML data source describing a library and its books.


<xml version="1.0"?>
<library>
<categories>
<category cid="1">Web Development</category>
<category cid="2">Database Programming</category>
<category cid="3">PHP</category>
<category cid="4">Java</category>
</categories>
<books>
<book>
<title>Apache 2</title>
<author>Peter Wainwright</author>
<publisher>Wrox</publisher>
<category>1</category>
</book>
<book>
<title>Advanced PHP Programming</title>
<author>George Schlossnagle</author>
<publisher>Developer Library</publisher>
<category>1</category>
<category>3</category>
</book>
<book>
<title>Visual FoxPro 6 - Programmers Guide</title>
<author>Eric Stroo</author>
<publisher>Microsoft Press</publisher>
<category>2</category>
</book>
<book>
<title>Mastering Java 2</title>
<author>John Zukowski</author>
<publisher>Sybex</publisher>
<category>4</category>
</book>
</books>
</library>

DOM

The DOM PHP extension allows operations on XML documents using the W3C DOM API. Before PHP 5, this was the only way with which PHP could access XML documents. If you have used DOM in Javascript, you will recognize that the object model is all but identical.


While the DOM method may be a long-winded way of traversing and manipulating an XML document, any DOM compliant code has the distinct advantage of being portable with any other API which implements the same W3C compliant object model.


In the example code below we use DOM to display information about each book. We first traverse the list of categories, loading their ID's and corresponding names into an indexed array. Then we display a short description for each book:

PHP:
<?php
    
/* here we must specify the version of XML : i.e: 1.0 */
    
$xml = new DomDocument('1.0');
    
$xml->load('xml/library.xml');

    
/* first to create a list of categories */
    
$categories = array();
    
$XMLCategories = $xml->getElementsByTagName('categories')->item(0);

    foreach(
$XMLCategories->getElementsByTagName('category') as $categoryNode) {
        
/* notice how we get attributes */
        
$cid = $categoryNode->getAttribute('cid');
        
$categories[$cid] = $categoryNode->firstChild->nodeValue;
    }
?>
<html>
    <head>
        <title>XML Library</title>
    </head>
    <body>
        <?php foreach($xml->getElementsBytagName('book') as $book):
            
/* find the title */
            
$title = $book->getElementsByTagName('title')->item(0)->firstChild->nodeValue;

            
/* find the author - for simplicity we assume there is only one */
            
$author = $book->getElementsByTagName('author')->item(0)->firstChild->nodeValue;

            
/* list categories */    
            
$bookCategories = $book->getElementsByTagName('category');

            
$catList = '';
            foreach(
$bookCategories as $category) {
                
$catList .= $categories[$category->firstChild->nodeValue] . ', ';
            }
            
            
$catList = substr($catList, 0, -2); ?>
            
        <div>
            <h2><?php echo($title) ?></h2>
            <p><b>Author:</b>: <?php echo($author) ?></p>
            <p><b>Categories: </b>: <?php echo($catList) ?></p>
        </div>    
        <?php endforeach; ?>
</html>

Again, modifying the XML is a little long winded. To add a category for example:

PHP:
function addCategory(DOMDocument $xml, $catID, $catName)
{
    
$catName = $xml->createTextNode($catName); // create a node to hold the text
    
$category = $xml->createElement('category'); // create a category element
    
$category->appendChild($catName); // add the text to the category element
    
$category->setAttribute('cid', $catID); // set the category id
        
    
$XMLCategories = $xml->getElementsByTagName('categories')->item(0);
    
$XMLCategories->appendChild($category); // add the new category