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 notation parameter identifies the notation declaration with which this unparsed<br />

entity is associated.<br />

Default Handler<br />

For any other event, such as the XML declaration and the XML document type, the<br />

default handler is called. To set the default handler, call the xml_set_default_<br />

handler( ) function:<br />

xml_set_default_handler(parser, handler);<br />

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

my_default_handler(parser, text);<br />

The text parameter will have different values depending on the kind of event triggering<br />

the default handler. Example 11-7 just prints out the given string when the<br />

default handler is called.<br />

Example 11-7. Default handler<br />

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

echo "XML: Default handler called with '$inData'\n";<br />

}<br />

Options<br />

The XML parser has several options you can set to control the source and target<br />

encodings and case folding. Use xml_parser_set_option( ) to set an option:<br />

xml_parser_set_option(parser, option, value);<br />

Similarly, use xml_parser_get_option( ) to interrogate a parser about its options:<br />

$value = xml_parser_get_option(parser, option);<br />

Character encoding<br />

The XML parser used by <strong>PHP</strong> supports Unicode data in a number of different character<br />

encodings. Internally, <strong>PHP</strong>’s strings are always encoded in UTF-8, but documents<br />

parsed by the XML parser can be in ISO-8859-1, US-ASCII, or UTF-8. UTF-16<br />

is not supported.<br />

When creating an XML parser, you can give it an encoding to use for the file to be<br />

parsed. If omitted, the source is assumed to be in ISO-8859-1. If a character outside<br />

the range possible in the source encoding is encountered, the XML parser will return<br />

an error and immediately stop processing the document.<br />

The target encoding for the parser is the encoding in which the XML parser passes<br />

data to the handler functions; normally, this is the same as the source encoding. At<br />

any time during the XML parser’s lifetime, the target encoding can be changed. Any<br />

270 | 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!