Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

272 CHAPTER 8 SIMPLE API FOR XML

Filed under: PHP and XML — webmaster @ 04:10

272 CHAPTER 8 SIMPLE API FOR XML (SAX) /* Set parser options */ xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, 0); /* Register handlers */ xml_set_element_handler($xml_parser, “startElement”, “endElement”); xml_set_character_data_handler ($xml_parser, “chandler”); /* Parse XML */ if (!xml_parse($xml_parser, $xml, 1)) { /* Gather Error information */ die(sprintf(”XML error: %s at line %d”, xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } /* Free parser */ xml_parser_free($xml_parser); ?> To begin examining this extension, you will skip the first step. It is quite difficult to attempt to write event-handling functions without even knowing what the events are and what parameters the functions need. Once the parser has been created and any parse options set, you will return to writing the handler functions. Listing 8-1 may also offer some insight into these functions prior to reaching the Event Handlers section. The Parser The parser is the focal point of this extension. Every built-in function for xml, other than the ones creating it and two encoding/decoding functions, requires the parser to be passed as a parameter. The parser, when created, takes the form of a resource within PHP 5, just as in PHP 4. The API was left unchanged, unlike the domxml extension, leaving the parser as a resource rather than adding an OOP interface. This not only allows no coding changes when moving from PHP 4 to PHP 5, but the extension already implements a way to use objects with the parser, which is discussed later in this chapter in the Using Objects and Methods section. Creating the Parser You create the parser using the function xml_parser_create(), which takes an optional parameter specifying the output encoding to use. Input encoding is automatically detected using either the encoding specified by the document or a BOM. When neither is detected, UTF-8 encoded input is assumed. Upon successful creation of the parser, it is returned to the application as a resource; otherwise, this function returns NULL. For example: if ($xml_parser = xml_parser_create()) { /* Insert code here */ } Upon successfully executing this code, the variable $xml_parsercontains the resource that will be used in the rest of the function calls within this extension.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Cheap Web Hosting