Reading XML with PHP, The XML Document

I have found many situations where being able to read the information in a XML/RSS file has come in handy. For example, putting a block on a page with the latest entries from a blog. Since XML files are basically structured flat databases, I use them sometimes for short collections that do not require security.

XML files are not difficult. You don’t need a fancy, expensive program to generate them, and PHP has functions built in to read and manipulate them.

Creating the XML File

Like HTML and XHTML files, XML has a structure, but other than the definition of the file type, the structure is completely up to you. Basically, you need the XML declaration at the top of the document:

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

From there, you create your own tags to create the structure of the file. There are some standard XML format layout, such as RSS, but you really do have a lot of freedom when creating XML files for your own use.

For the purpose of this tutorial, we will create an XML file that will contain information about some of my favorite web resources. Before we actually start building the file, we need to think about what information we need it to contain. For each documentation source, we need a name or title and a link to the resource. Also, I would like to group the like sources into categories. To start off, we need a “root” or encapsulating tag. Think of the root as the being like the html or body tags in HTML or XHTML. We will call our root tag documents.

<?xml version="1.0" encoding="UTF-8"?>
<documents>
</documents>

Everything will go inside these open/close tags.

Next, we will add open/close tags for our first category. We will also give it a name.

<?xml version="1.0" encoding="UTF-8"?>
<documents>
<category name="PHP">
</category>
</documents>

Notice I put the name in as an attribute of the tag. This is important when we use PHP to extract the information, as we will extract it differently than we will the other information. Now, we can add our first reference. I will refer to them using a tag I named doc.

<?xml version="1.0" encoding="UTF-8"?>
<documents>
<category name="PHP">
<doc>
<name>PHP Manual</name>
<link>http://www.php.net/manual/en/</link>
</doc>
</category>
</documents>

We have openning and closing doc tags. Inside it we have tags for the name of the resource and another for the link to the resource. We can continue on with more categories and resources, until we have our completed list of resources. My complete XML document looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<documents>
<category name="PHP">
<doc>
<name>PHP Manual</name>
<link>http://www.php.net/manual/en/</link>
</doc>
</category>
<category name="jQuery">
<doc>
<name>jQuery Documentation</name>
<link>http://docs.jquery.com/Main_Page</link>
</doc>
<doc>
<name>jQuery UI</name>
<link>http://jqueryui.com/</link>
</doc>
<doc>
<name>Visual jQuery</name>
<link>http://www.visualjquery.com/</link>
</doc>
</category>
<category name="HTML/CSS">
<doc>
<name>SitePoint HTML Reference</name>
<link>http://reference.sitepoint.com/html</link>
</doc>
<doc>
<name>SitePoint CSS Reference</name>
<link>http://reference.sitepoint.com/css</link>
</doc>
<doc>
<name>HTML/XHTML Templates</name>
<link>http://www.webstandards.org/learn/reference/templates/</link>
</doc>
</category>
<category name="Toolbox">
<doc>
<name>Image to Color Palette</name>
<link>http://www.cssdrive.com/imagepalette/</link>
</doc>
<doc>
<name>Kuler</name>
<link>http://kuler.adobe.com/</link>
</doc>
<doc>
<name>Typetester</name>
<link>http://www.typetester.org/</link>
</doc>
<doc>
<name>Better CSS Font Stacks</name>
<link>http://www.codestyle.org/css/font-family/BuildBetterCSSFontStacks.shtml</link>
</doc>
<doc>
<name>RegEx Tester</name>
<link>http://www.quanetic.com/Regex</link>
</doc>
</category>
</documents>

In the next part, we will read the XML document we just created using PHP.

Part 2, Extracting the XML with PHP

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s