05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 11-8. Creating a parser (continued)<br />

xml_set_processing_instruction_handler($parser, 'processing_instruction');<br />

xml_set_default_handler($parser, 'default');<br />

return array($parser, $fp);<br />

}<br />

function parse ($parser, $fp) {<br />

$blockSize = 4 * 1024; // read in 4 KB chunks<br />

while($data = fread($fp, $blockSize)) { // read in 4 KB chunks<br />

if(!xml_parse($parser, $data, feof($fp))) {<br />

// an error occurred; tell the user where<br />

echo 'Parse error: ' . xml_error_string($parser) . " at line " .<br />

xml_get_current_line_number($parser));<br />

return FALSE;<br />

}<br />

}<br />

return TRUE;<br />

}<br />

if (list($parser, $fp) = create_parser('test.xml')) {<br />

parse($parser, $fp);<br />

fclose($fp);<br />

xml_parser_free($parser);<br />

}<br />

Errors<br />

The xml_parse( ) function will return true if the parse completed successfully or<br />

false if there was an error. If something did go wrong, use xml_get_error_code( ) to<br />

fetch a code identifying the error:<br />

$err = xml_get_error_code( );<br />

The error code will correspond to one of these error constants:<br />

XML_ERROR_NONE<br />

XML_ERROR_NO_MEMORY<br />

XML_ERROR_SYNTAX<br />

XML_ERROR_NO_ELEMENTS<br />

XML_ERROR_INVALID_TOKEN<br />

XML_ERROR_UNCLOSED_TOKEN<br />

XML_ERROR_PARTIAL_CHAR<br />

XML_ERROR_TAG_MISMATCH<br />

XML_ERROR_DUPLICATE_ATTRIBUTE<br />

XML_ERROR_JUNK_AFTER_DOC_ELEMENT<br />

XML_ERROR_PARAM_ENTITY_REF<br />

XML_ERROR_UNDEFINED_ENTITY<br />

XML_ERROR_RECURSIVE_ENTITY_REF<br />

XML_ERROR_ASYNC_ENTITY<br />

272 | Chapter 11: XML<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!