Page 622 - Beginning PHP 5.3
P. 622
Part III: Using PHP in Practice
Parsing the XML Document
Now you ’ re ready to actually parse the document. First, if the document is a file on disk or at a URL, you
need to read its contents into a string variable using, for example, PHP ’ s file_get_contents()
function. Once you have your XML in a string variable, call xml_parse() , passing in the parser resource
you created earlier, as well as the string variable name. For example:
$xml = file_get_contents( “xml_document.xml” );
xml_parse( $parser, $xml );
The parser then processes the contents of the string variable, calling your event handler functions as
appropriate, until it ’ s finished reading all the XML data. If the parser managed to parse all the data
successfully it returns true ; otherwise it returns false .
You can find more about file_get_contents() and other file - related functions in Chapter 11.
You can parse XML data in chunks if you prefer; this is useful if you have a lot of XML data and you ’ d
rather not read it all into memory in one go. To do this, just keep calling xml_parse() with the new
chunk of data as the second argument. When passing the last chunk of data, pass a third value of true
to xml_parse() to tell it that it ’ s reached the end of the XML:
xml_parse( $parser, $xml, true );
Once you ’ ve parsed your XML, it ’ s a good idea to delete the parser to free up memory. To do this, use
xml_parser_free() , as follows:
xml_parser_free( $parser );
Dealing with Parse Errors
If the call to xml_parse() returns false , there was a problem parsing the XML document. You can find
out the exact cause of the problem by calling various XML Parser functions:
Function Description
xml_get_error_code( $parser ) Returns an error code indicating the last error
xml_error_string( $code ) Returns the error string associated with the
supplied error code
xml_get_current_line_number( $parser ) Returns the line number of the currently parsed
line in the XML document (this will be the line
where the error occurred)
xml_get_current_column_number( $parser ) Returns the currently parsed column number
in the XML document (the point where the
error occurred)
584
9/21/09 9:17:46 AM
c19.indd 584
c19.indd 584 9/21/09 9:17:46 AM