Page 612 - Beginning PHP 5.3
P. 612
Part III: Using PHP in Practice
PHP has many features and functions that make working with XML data fast and efficient, as well as
intuitive. In this chapter you learn the basics of XML, and how to create XML documents from scratch.
You then move onto using PHP ’ s XML Parser extension to read and parse XML documents
programmatically.
Once you ’ ve mastered XML Parser, you explore PHP ’ s DOM extension that gives you a lot of power to
read, create, and manipulate XML documents; then you take a look at SimpleXML — a nice, easy way
to read and perform simple operations on XML data. Finally, you take a brief look at another aspect of
XML called XSL, and examine PHP ’ s XSLTProcessor class for transforming XML documents into
other formats.
What Is XML?
XML is a specification for creating your own markup languages. In turn, you use these markup
languages to create documents. Like HTML, an XML document contains elements and attributes in the
form of tags.
Though XML documents are human - readable, many applications are designed to parse XML documents
automatically and work efficiently with their content. PHP has many XML - related functions that can
easily be used to work with XML documents, or transform non - XML data into XML documents.
You can make your own XML document as easily as this:
< ?xml version=”1.0” ? >
< stockList >
< item type=”fruit” >
< name > apple < /name >
< unitPrice > 0.99 < /unitPrice >
< quantity > 412 < /quantity >
< /item >
< item type=”vegetable” >
< name > beetroot < /name >
< unitPrice > 1.39 < /unitPrice >
< quantity > 67 < /quantity >
< /item >
< /stockList >
The first line of this document is called the XML declaration ; it indicates that the following lines comprise
an XML document, and specifies the version of XML that is used to create the document. The second line
defines the root element of the document (named stockList ). There can be only one root element for an
XML document. The third line defines a child element of the root element, named item , and it contains an
attribute named type that is set to the value fruit .
From reading this XML document, you can tell that:
❑ It stores a list of stock items
❑ There are 412 apples available, and an apple is a fruit and costs $0.99
❑ There are 67 beetroots available, and a beetroot is a vegetable and costs $1.39
574
9/21/09 9:17:43 AM
c19.indd 574
c19.indd 574 9/21/09 9:17:43 AM