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.

If your external entity reference handler returns a false value (which it will if it<br />

returns no value), XML parsing stops with an XML_ERROR_EXTERNAL_ENTITY_HANDLING<br />

error. If it returns true, parsing continues.<br />

Example 11-6 shows how you would parse externally referenced XML documents.<br />

Define two functions, create_parser( ) and parse( ), to do the actual work of creating<br />

and feeding the XML parser. You can use them both to parse the top-level document<br />

and any documents included via external references. Such functions are<br />

described later, in “Using the Parser.” The external entity reference handler simply<br />

identifies the right file to send to those functions.<br />

Example 11-6. External entity reference handler<br />

function external_entity_reference($inParser, $inNames, $inBase,<br />

$inSystemID, $inPublicID) {<br />

if($inSystemID) {<br />

if(!list($parser, $fp) = create_parser($inSystemID)) {<br />

echo "Error opening external entity $inSystemID \n";<br />

return false;<br />

}<br />

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

}<br />

return false;<br />

}<br />

Unparsed entities<br />

An unparsed entity declaration must be accompanied by a notation declaration:<br />

<br />

]><br />

Register a notation declaration handler with xml_set_notation_decl_handler( ):<br />

xml_set_notation_decl_handler(parser, handler);<br />

The handler will be called with five parameters:<br />

my_notation_handler(parser, notation, base, system, public);<br />

The base parameter is the base URI for resolving the identifier of the notation (which<br />

is currently always empty). Either the system identifier or the public identifier for the<br />

notation will be set, but not both.<br />

Register an unparsed entity declaration with the xml_set_unparsed_entity_decl_<br />

handler( ) function:<br />

xml_set_unparsed_entity_decl_handler(parser, handler);<br />

The handler will be called with six parameters:<br />

my_unp_entity_handler(parser, entity, base, system, public, notation);<br />

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

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Parsing XML | 269

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

Saved successfully!

Ooh no, something went wrong!