I l @ ve RuBoard |
During the initial stages of application development, it's possible to get by without using error handlers in your code; however, as an application is being packaged for release, graceful error handling and recovery becomes a must. PHP allows developers to accomplish this error handling via its xml_get_error_code() function (which prints the error code returned by the parser when it hits a bump) and its xml_error_string() function, (which returns a short, human-readable error message corresponding to the error code). Table 2.2 is a list of error codes and their corresponding named constants, together with what they mean. Table 2.2. SAX Parser Error Codes
In order to illustrate how this error-handling works, consider Listing 2.19, which contains a badly formed XML document. (There's a duplicate attribute within the element <circle> .) Note that the xml_error_string() function has been used to return a more helpful description of the error. Listing 2.19 Better Error Handling<html> <head> <basefont face="Arial"> </head> <body> <?php // XML data $xml_data = <<<EOF <?xml version="1.0"?> <shape> <circle color="red" radius="5" x="14" y="574" color="red" /> </shape> EOF; // define handlers function startElementHandler($parser, $name, $attributes) { // code snipped out } function endElementHandler($parser, $name) { // code snipped out } // initialize parser $xml_parser = xml_parser_create(); // set callback functions xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler"); if (!xml_parse($xml_parser, $xml_data)) { $ec = xml_get_error_code($xml_parser); die("XML parser error (error code $ec): " . xml_error_string($ec)); } // all done, clean up! xml_parser_free($xml_parser); ?> </body> </html> Why stop there? It's also possible to include information on where exactly the error occurred within the document, with these three built-in functions:
Listing 2.20 is the revised script that incorporates this information. Listing 2.20 Adding Line and Column Information to Error Messages<html> <head> <basefont face="Arial"> </head> <body> <?php // XML data $xml_data = <<<EOF <?xml version="1.0"?> <shape> <circle color="red" radius="5" x="14" y="574" color="red" /> </shape> EOF; // define handlers function startElementHandler($parser, $name, $attributes) { // code snipped out } function endElementHandler($parser, $name) { // code snipped out } // initialize parser $xml_parser = xml_parser_create(); // set callback functions xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler"); if (!xml_parse($xml_parser, $xml_data)) { $ec = xml_get_error_code($xml_parser); die("XML parser error (error code $ec): " . xml_error_string($ec) . "<br>Error occurred at line " . xml_get_current_line_number($xml_parser) . ", column " . xml_get_current_column_number($xml_parser) . ", byte offset " . xml_get_current_byte_index($xml_parser)); } // all done, clean up! xml_parser_free($xml_parser); ?> </body> </html> And here's the output: XML parser error (error code 8): duplicate attribute Error occurred at line 3, column 46, byte offset 78 There! Isn't that much more helpful? |
I l @ ve RuBoard |