XML Through PHP: Dynamic and Portable Data

Ranked #88,784 in Computers & Electronics, #1,381,149 overall

Why XML and PHP?

XML was created to make it easy to port data from one form to another. It is great at that job - evidenced by the numerous feeds and XML data files floating around the internet.

However, a developer needs a way to interact with XML. You could write your own XML parser, but PHP has done half the work for you. PHP 5 comes with built-in support for XML - in the form of SimpleXML - that makes it a piece of cake for any developer to read, modify, and create XML files with a web-based application.

What is XML?

Simply put, XML is a standard way to store data.

The acronym XML stands for Extensible Markup Language. In many ways it is similar to its more well-known cousin, Hypertext Markup Language (HTML). Yet XML was designed for a specific purpose - to help make data accessible across different platforms and systems.

XML achieves this by adhering to a strict set of standards about document formation. When you create an XML document you need to follow the rules - so other XML applications can read your document.

You could use XML to store all kinds of information - from word documents to blog feeds to database query results. XML sets the base - and developers fill in the uses.

How Do You Create an XML File?

XML, like HTML, is made up of a bunch of tags that contain information. The difference is that XML tags describe and organize the information, while HTML tags determine how the information is displayed.

An XML file is created by building a series of tags that hold information - also called elements.

One element can have another element inside of it. The first element is called the parent while the second element is called the child.

The document starts with one root level element. It can then be made up of as many child elements - each with its own child elements - as you want.

For example, your XML file could describe a list of books on your shelf. The parent element might titled "library." The library could be made up of a series of child elements called "book." Each book would have its own child elements, like "author," "title," "copyright date," "publisher," etc.

You can read through the current XML standard for more info on file structure.

Creating a Custom RSS Feed

Use PHP to Syndicate Your Content

One of the greatest uses for XML is to create RSS feeds. Most websites today have RSS feeds of their latest articles - which users can then subscribe to with a variety of feed readers.

If you're using a CMS or a blogging platform, this may be done for you automatically. If you created your website from scratch, though, you'll need to write your own script to create an RSS feed for your site.

Given the necessary information (article titles, links, descriptions, and publishing dates), this is pretty simple.

The structure of an RSS feed can vary somewhat, but here's the basic structure.

The root element of the RSS feed is a "channel." That channel has some child elements that define its characteristics - usually including a "title," "description," "link," and a "lastBuildDate."

The "channel" element also has a series of "item" elements - each one representing one article in the feed. These "items" can have a number of optional child elements, usually including a "title," "pubDate," "link," and "description."

For a complete list of optional elements you can include in a feed, look through the RSS 2.0 Specification.

Here's an outline of what you need to do to create an RSS feed in PHP.
  • Query the DB for information on recent articles
  • Build a string with the XML data, formatted into the proper elements
  • Write the string to an xml file using file_put_contents


You could read this tutorial on creating RSS feeds for a guide to fleshing out this script idea. This other tutorial explores using SimpleXML to create an RSS data feed.

Syndicating Other Feeds on Your Website

Another great use for XML is to read someone else's feed and use it to syndicate links to relevant content on your site.

Since RSS feeds are built with a standard structure, we know we should be able to find all the elements we need to create a link back to an article (title, link). All we need to do is somehow read the XML file and access that relevant information.

Enter Simple XML. PHP 5 comes with a built in library for working with XML - SimpleXML.

You can load an XML file with the function call "simplexml_load_file($filename)".

This creates an series of objects connected and organized according to the structure of the XML file. The root object represents the root element, and it has an object to represent each of its children elements. Likewise, those children have other objects that represent their children.

You can read this tutorial for a step-by-step guide on how to parse an RSS feed and publish the links on your site.

Working with Attributes - If You Have To

Normally, an XML document is made up of a bunch of elements. However, you can also define attributes for those elements.

For example, inside of a "book" tag, we could define the attribute 'copyright="1957"'. This is more or less equivalent to adding a child element, naming it copyright, and giving it the content 1957.

So why do people use attributes? I don't know. I never do - because they make it much harder to access the information with an XML parser.

Basic XML parsers do three things. First, they scan for opening tags/elements. Then, they spit out the contents. Finally, they scan for closing tags.

These basic parsers more or less ignore attributes - making it hard to access the information and manipulate it.

However, SimpleXML does make an accommodation for this. If an element has any defined attributes, they are exploded into an array, indexed by the names of the attributes and attached to that element.

It's best not to use these attributes and to keep your information contained in elements. If you have to work with attributes, though, you can read this guide to using SimpleXML to access attribute data.

Other Uses for XML

Besides RSS feeds, there are plenty of other implementations for XML files.

DiggAPI

Digg recently introduced an API for users to access lists of articles and content. By building a query string and accessing the given location, a developer can get an XML response with the necessary information. That XML can then be parsed to display the Digg data on a different website.

WoW Character Profiles

The WoW Armory Character Profiles are XML files with special stylesheets for formatting. You could access this xml file, parse it, and use it to display your character's information elsewhere - like on a forum userbar.

Storage Alternative to mySQL

Although mySQL is great, sometimes you don't want to use a database. An XML file can be a superior alternative to a simple text file or comma delimited file. This Shoutbox script is one example of how to do this.

New Guestbook

submit

by

Walkere

Although I'm a High School Social Studies teacher by day, computers and programming have been a lifelong passion of mine.
I spend a good deal of my s...
more »

Feeling creative? Create a Lens!