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.

The end element handler is called when the parser encounters the end of an element:<br />

my_end_element_handler(parser, element);<br />

It takes two parameters: a reference to the XML parser calling the handler, and the<br />

name of the element that is closing.<br />

Example 11-3 shows an end element handler that formats the element.<br />

Example 11-3. End element handler<br />

function end_element($inParser, $inName) {<br />

echo '&lt;/$inName&gt;';<br />

}<br />

Character Data Handler<br />

All of the text between elements (character data, or CDATA in XML terminology) is<br />

handled by the character data handler. The handler you set with the xml_set_<br />

character_data_handler( ) function is called after each block of character data:<br />

xml_set_character_data_handler(parser, handler);<br />

The character data handler takes in a reference to the XML parser that triggered the<br />

handler and a string containing the character data itself:<br />

my_character_data_handler(parser, cdata);<br />

Example 11-4 shows a simple character data handler that simply prints the data.<br />

Example 11-4. Character data handler<br />

function character_data($inParser, $inData) {<br />

echo $inData;<br />

}<br />

Processing Instructions<br />

Processing instructions are used in XML to embed scripts or other code into a document.<br />

<strong>PHP</strong> code itself can be seen as a processing instruction and, with the tag style, follows the XML format for demarking the code. The XML parser calls<br />

the processing instruction handler when it encounters a processing instruction. Set<br />

the handler with the xml_set_processing_instruction_handler( ) function:<br />

xml_set_processing_instruction(parser, handler);<br />

A processing instruction looks like:<br />

<br />

The processing instruction handler takes in a reference to the XML parser that triggered<br />

the handler, the name of the target (for example, “php”), and the processing<br />

instructions:<br />

my_processing_instruction_handler(parser, target, instructions);<br />

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

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

Parsing XML | 267

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

Saved successfully!

Ooh no, something went wrong!