What is XML?

The eXtensible Markup Language is a way to structure your data for sharing across sites. Some of the technologies that are crucial to the web like RSS (Real Simple Syndication) and Podcasts are special flavors of XML. The beautiful thing about XML is that you can easily roll your own for anything you need.

XML is easy to create because it’s a lot like HTML…except you can make up your own tags. Let’s say, for example that you’re putting together a feed for a list of songs playing at your own radio station. We’ll keep this simple, so we’ll just encode the name of the artist, the title of the song, plus the time when the song was played. We make up a couple of tags called <title> and <artist> and wrap each of them around a <song> tag. We’ll create a dateplayed attribute for each song with the date and time the song was played. You might encode something like that in this manner.

There’s some rules that you have to adhere to when creating XML data. If you’re familiar with XHTML…you’ll be right at home with some of these, but let’s review them:

  • XML is case sensitive so <Title>` is not the same as <title>.
  • All XML elements must have closing tags.
  • XML requires a root element (the <songs> tag above serves as our root element)
  • Attributes must be quoted
  • Special characters (like & (&amp;) and < (&lt;) and > (&gt;) signs) must be encoded.

Example -

What is an XML Parser?

To read and update, create and manipulate an XML document, you will need an XML parser. In PHP there are two major types of XML parsers:

  • Tree-Based Parsers
  • Event-Based Parsers