Chapter 9: Advanced PHP Techniques: Working with XML

This short chapter will introduce the basic concepts of working with XML and PHP, but in no way should this be considered a definitive guide on the subject! There are entire books written on XML, and, in fact, entire books written on the pairing of XML and PHP. Use this chapter as a primer for a subject you may decide to explore further on your own.

XML Overview

The name XML comes from the full name of the language, Extensible Markup Language. Although "markup" is in its name, do not think of XML like you do HTML, because aside from the fact that both languages are based on tag pairs, there are no similarities. At its core, XML is a method of data exchange, in that it holds well-defined content within its boundaries. HTML, on the other hand, couldn't care less what is contained in the content or how it is structured—its only purpose is to display the content to the browser. XML is used to define and carry the content, while HTML is used to make it "pretty."

This is not to say that XML data cannot be made pretty, or that you cannot display XML data in your Web browser. You can, in fact, do so, using Extensible Style Language (XSL) and Cascading Style Sheets (CSS) to transform the content into a browser-renderable format while still preserving the content categorization. For example, say you have an area on your Web site reserved for recent system messages, and those items each contain the following:

  • title

  • message

  • author

  • date of message

The HTML used to display this information may have the title in bold tags, the message inside paragraph containers, the author's name in italics and the date in a small font. XML, on the other hand, is more concerned with knowing that there are four distinct content elements. The categorization of content is crucial to data exchange—by separating the data and its structure from the presentation elements, you can use the content however you wish, and are not limited to the particular marked-up style that static HTML has forced on you.

Basic XML Document Structure

Before moving forward into working with XML documents, you need to know exactly how to create them! XML documents contain two major elements, the prolog and the body. The prolog contains the XML declaration statement (much like an HTML document type definition statement), and any processing instructions and comments you may want to add. In this example, you'll just use the declaration statement and a comment.


For a complete definition of XML documents, read the XML Specification at

Using the system message example from the previous section, you'll create a file called messages.xml and begin it with

 <?xml  version="1.0" ?> <!--Sample XML document --> 

In the declaration, only the version is required, but you can also add an encoding value and a standalone value. For example:

 <?xml  version="1.0" encoding="UTF-8" standalone="yes" ?> 

All the fun begins in the body area of the document, where the content structure is contained. XML is hierarchical in nature. Think of it like a book—a book has a title, and it contains chapters, each of which contain paragraphs, and so forth. There is only one root element in an XML document—using the book example, the element may be called Book and the tags <Book></Book> surround all other information.

Using the system messages example, call the element SystemMessage and add an open tag to your messages.xml document:


Next, add all subsequent elements—called children—to your document. In this example, you need title, body, author, and date information. Create children elements called MessageTitle, MessageBody, MessageAuthor, and MessageDate. But wait a minute—for the author information, what if you want both the name and an email address? Not a problem—you just create another set of child elements within your parent element (which just happens to be a child element of the root element):

 <MessageAuthor> <MessageAuthorName>Joe SystemGod</MessageAuthorName> <MessageAuthorEmail></MessageAuthorEmail> </MessageAuthor> 

All in all, your messages.xml document could look like this:

 <?xml  version="1.0" ?> <!--Sample XML document --> <SystemMessage>         <MessageTitle>System Down for Maintenance</MessageTitle>         <MessageBody>This system will be going down for maintenance between 3am and 5am GMT  on February 10th.</MessageBody>        <MessageAuthor>                <MessageAuthorName>Joe SystemGod</MessageAuthorName>                <MessageAuthorEmail></MessageAuthorEmail>        </MessageAuthor>        <MessageDate>Feb 8th, 2003</MessageDate> </SystemMessage> 

Here are two very important rules to keep in mind for creating valid XML documents:

  • XML is case-sensitive. <Book> and <book> are different elements.

  • All XML tags must be properly closed. XML tags must be properly nested, and no overlapping tags are allowed.

Put the messages.xml file (or one like it) on your Web server for use in later examples. As a side note, if you have the Microsoft Internet Explorer Web browser, you can actually view your XML document in a tree-like format. This browser uses an internal stylesheet to provide a default tree view of valid XML documents. Figures 9.1 and 9.2 show the original view (all elements opened), as well as a view with the MessageAuthor element collapsed.

click to expand
Figure 9.1: All XML elements displayed

click to expand
Figure 9.2: One collapsed element

XML and Traditional Databases

Based on the description of XML given so far, a natural question is, "Is XML a database?" This is a very good question indeed. The simple answer is "sort of," but that's not really a technical explanation. If a database is defined simply as a collection of data, then sure—XML is a database because it categorizes and holds information in a centralized place.

When compared to a database management system, such as MySQL, Oracle, Microsoft SQL Server, and so forth, the differences become obvious. XML stores categorized content, you can interface with it (as we will do in this chapter), and you can move it from location to location. However, as XML is simply a text file residing on your file system, it does not have the efficiency, security, and accessibility that the traditional database system has.

For more on XML and databases—the pros, cons, differences and so forth—go to, where a man named Ronald Bourret has posted some exceptional introductory documents. For the remainder of this chapter, just focus on XML being used as a portable data container.

PHP Essentials
PHP Essentials, 2nd Edition
ISBN: 1931841349
EAN: 2147483647
Year: 2002
Pages: 74 © 2008-2017.
If you may any questions please contact us: