The Extensible Markup Language is definitely something that most developers will
want to add to their toolbox. XML is a W3C standard that is open, language
neutral, API-neutral, streamable, textual, human-readable and is a way of
bringing structured data to the web. XML is a subset of SGML and is not a
markup language in itself but allows the author to define their own markup
language where it is best suited for representing hierachical data.
Now, parsing XML documents with PHP is not a topic that has been covered
extensively as far as I have seen on the web and elsewhere. There is some
very useful information in the PHP manual on XML parsing functions but this
seems to be all the information that I could find. Other languages seem to
have much more information and working examples of using XML than PHP does,
so in this article I will attempt to do my part to change this.
I will walk the reader through a fairly simple application of XML that I used
to implement a news system for my home page. I actually use this on my
and it works
really well. Feel free to use it on yours if you like. Ok, let's get started !
To use the XML parsing functions available to PHP you will need to have a
module that supports XML installed on your web server. This means you will
probably have to recompile your module to support XML, please look
for more info on how
to do this. The XML parsing functions are really wrappers around the expat
parser which is a SAX parser, which means Simple API for XML. The other kind
of parser is a DOM parser, easier to use, an example of this is Microsoft's
MSXML component which lets the programmer navigate through a tree style object
accessing nodes and elements. The way the expat parser (or any SAX parser)
allows you to parse an XML document is to specify callback functions for
different tag types in the XML document being parsed. When the parser starts
parsing your XML document and encounters a tag it will call your function and
process the tag as specified in your function before continuing. You could
look at this as an event driven way of parsing.