Page 620 - Beginning PHP 5.3
P. 620
Part III: Using PHP in Practice
Reading XML Documents with PHP
Recently, as the XML specification has gained prominence as a means of exchanging and storing data,
PHP has added progressively more functions and classes to make it easier to work with XML documents.
In the remainder of this chapter you concentrate on the following XML features in PHP:
❑ Reading, or parsing, XML documents using the XML Parser extension
❑ Using the DOM extension to manipulate XML documents via the Document Object Model
❑ Reading, writing, and manipulating XML documents using PHP ’ s SimpleXML extension
This section looks at reading XML documents with XML Parser.
How XML Parser Works
With XML Parser, you create functions to deal with specific events — such as when the start or end of an
XML element is encountered — then register these functions as event handlers for the parser. Then,
whenever a parser encounters a new piece of the XML document, it calls your appropriate event handler
function which, in turn, processes that piece of the document.
The process of using XML Parser to read an XML document usually breaks down like this:
1. Create a new parser resource by calling the xml_parser_create() function.
2. Create two event handler functions to handle the start and end of an XML element, then register
these functions with the parser using the xml_set_element_handler() function.
3. Create another event handler function to handle any character (text) data that may be found
inside an element, and register this function with the parser using
xml_set_character_data_handler() .
4. Parse the XML document by calling the xml_parse() function, passing in the parser and the
XML string to parse.
5. Finally, destroy the parser resource, if it ’ s no longer needed, by calling xml_parser_free() .
Next you explore each of these steps more closely.
Creating a New Parser
The process of creating a new parser is easy. Simply call xml_parser_create() to generate a new
parser resource, and store the resource in a variable:
$parser = xml_parser_create();
You can optionally add an argument that specifies the encoding in which character data is passed to
your event handler functions. By default, the parser sends characters using UTF - encoding, but you can
8
1
change this to either ISO - 8859 - or US - ASCII if you prefer. For example:
$parser = xml_parser_create( “US-ASCII” );
582
9/21/09 9:17:45 AM
c19.indd 582
c19.indd 582 9/21/09 9:17:45 AM