PARSING XML WITH PHP, PART 1 One of the advantages of XML is that you can use it for communicating between organizations across common and standard technologies such as the Internet. The PHP script language is a popular tool that is used for server-side scripting. Organizations that have an existing investment in PHP technology can leverage that to provide XML capabilities. Using PHP, you can build Web-enabled applications that can process XML documents. PHP'S XML CAPABILITIES PHP uses the Expat XML processor from Ginger Alliance to provide the underlying XML parsing implementation. PHP also contains support for XSLT templates using Ginger Alliance's Sablotron product, allowing for more advanced document manipulation. The Expat parser is an event-based XML parser, as opposed to a tree-based parser. The Document Object Model (DOM) tree-based parser method is currently available in PHP on an experimental basis using the GNOME XML library. http://click.online.com/Click?q=69-CY-iIoWyS5iw2fZMoxItO-RLP-4R Using the Expat library, PHP developers can implement event-based parsers in their PHP code. PHP provides a compact set of functions for controlling the behavior of the parser as well as for registering callback functions. The callback functions are triggered when particular events occur based on the XML document. The types of events that can be triggered are start and end elements, nonelement character data (such as white space between elements), processing instructions, and a few others. SETTING UP AN XML PARSER IN PHP It's relatively simple to create your own XML parser using PHP. Using just a few of the XML functions, you can have a fully functioning XML parser in little time. We'll create a sample program that illustrates how to trigger events with XML tags and how to respond to those events using the parser callback functions. Before we get started, you should be aware of the concept of "case folding." This is the process where the case of XML elements is altered when the callback functions are called. By default, all element names are converted to uppercase when passed to the callback functions. Using the xml_parser_set_option() function, you can disable case folding so that element names remain intact. For example: xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); CREATING THE CALLBACKS First, we'll need to create the callback functions that will handle the events as they occur. There are two event handlers to create. The first will handle start element tags, and the other will handle end element tags. This corresponds to the start and end element tags in an XML document. For example, an element called will trigger the start element callback function when the tag occurs in the document. When the tag occurs, the end element callback function will be called. These functions must adhere to a specific parameter signature in order to accept the data coming from the XML parser. The start element function takes three parameters: the parser, the tag name, and the attributes. For example: function myStartHandler($parser, $tag, $attribs) { } The end element function takes two parameters. Because it's the end of the tag, there are no attributes; therefore, the end element function accepts a parser parameter and a tag name parameter. For example: function myEndHandler($parser, $tag) { } Once the functions are defined, they have to be registered to accept the callbacks. This is handled with PHP's xml_set_element_handler() function. This function takes three parameters: the parser, the start tag handler function, and the end tag handler function. For example: xml_set_element_handler($parser, "myStartHandler", "myEndHandler"); Now, the element handlers only handle actual element events. Any data that is not part of the element tag comes out as character data (like white space and element content data). There is a separate handler for this data called the character data handler, and it expects its own callback function. For example: xml_set_character_data_handler($parser, "myDataHandler"); The character data handler has a specified function signature like the element handlers. In this case, it should accept the parser and the data as parameters. For example: function myCharacterDataHandler($parser, $data) { } You must define the parser variable before these functions can use it. To create a new parser, use PHP's xml_parser_create() function, as follows: $parser = xml_parser_create(); When your PHP program is finished with the XML parser, it should free the resources associated with the parser. This is accomplished by using the xml_parser_free() function. For example: xml_parser_free($parser); Next week, I'll explain how to initiate the parsers and then put all the pieces together. Brian Schaffner is a senior consultant for DMR Consulting, a Fujitsu company. He provides architecture, design, and development support for DMR's Telcom360 group. ----------------------------------------