Page 333 - Open Soource Technologies 304.indd
P. 333
Unit 13: Extensible Markup Language
xml_set_default_handler(parser, handler); Notes
The handler will be called with two parameters:
my_default_handler(parser, text);
The text parameter will have different values depending on the kind of event triggering the
default handler. Example just prints out the given string when the default handler is called.
Default handler.
function default($inParser, $inData)
{
echo “<font color=\”red\”>XML: Default handler called with ‘$inData’</font>\n”;
}
Options
The XML parser has several options you can set to control the source and target encodings and
case folding. Use xml_parser_set_option( ) to set an option:
xml_parser_set_option(parser, option, value);
Similarly, use xml_parser_get_option( ) to interrogate a parser about its options:
$value = xml_parser_get_option(parser, option);
13.4.2 Character Encoding
The XML parser used by PHP supports Unicode data in a number of different character encodings.
Internally, PHP’s strings are always encoded in UTF-8, but documents parsed by the XML parser
can be in ISO-8859-1, US-ASCII, or UTF-8. UTF-16 is not supported.
When creating an XML parser, you can give it an encoding to use for the file to be parsed. If
omitted, the source is assumed to be in ISO-8859-1. If a character outside the range possible in
the source encoding is encountered, the XML parser will return an error and immediately stop
processing the document.
The target encoding for the parser is the encoding in which the XML parser passes data to the
handler functions; normally, this is the same as the source encoding. At any time during the
XML parser’s lifetime, the target encoding can be changed. Any characters outside the target
encoding’s character range are demoted by replacing them with a question mark character (?).
Use the constant XML_OPTION_TARGET_ENCODING to get or set the encoding of the text
passed to callbacks. Allowable values are: “ISO-8859-1” (the default), “US-ASCII”, and “UTF-8”.
13.4.3 Case Folding
By default, element and attribute names in XML documents are converted to all uppercase. You
can turn off this behavior (and get case-sensitive element names) by setting the XML_OPTION_
CASE_FOLDING option to false with the xml_parser_set_option( ) function:
xml_parser_set_option(XML_OPTION_CASE_FOLDING, false);
13.4.4 Using the Parser
To use the XML parser, create a parser with xml_parser_create( ), set handlers and options on
the parser, then hand chunks of data to the parser with the xml_parse( ) function until either
the data runs out or the parser returns an error. Once the processing is complete, free the parser
by calling xml_parser_free( ).
LOVELY PROFESSIONAL UNIVERSITY 327