In this third part of a five-part series on Web Services, we'll wrap up our discussion of MagpieRSS and move on to SimpleXML, an intuitive methodology for processing XML structures. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
Everyone agrees that XML signifies an enormous leap forward in data management and application interoperability. Yet how come it’s so darned hard to parse? Although powerful parsing solutions are readily available, DOM, SAX, and XSLT to name a few, each presents a learning curve that is just steep enough to cause considerable gnashing of the teeth among those users interested in taking advantage of XML’s practicalities without an impractical time investment. Leave it to an enterprising PHP developer (namely, Sterling Hughes) to devise a graceful solution. SimpleXML offers users a very practical and intuitive methodology for processing XML structures and is enabled by default as of PHP 5. Parsing even complex structures becomes a trivial task, accomplished by loading the document into an object and then accessing the nodes using field references, as you would in typical object-oriented fashion.
The XML document displayed in Listing 20-4 is used to illustrate the examples offered in this section.
Listing 20-4. A Simple XML Document
<?xml version="1.0" standalone="yes"?> <library> <book> <title>Pride and Prejudice</title> <author gender="female">Jane Austen</author> <description>Jane Austen's most popular work.</description> </book> <book> <title>The Conformist</title> <author gender="male">Alberto Moravia</author> <description>Alberto Moravia's classic psychological novel.</description> </book> <book> <title>The Sun Also Rises</title> <author gender="male">Ernest Hemingway</author> <description>The masterpiece that launched Hemingway's career.</description> </book> </library>