PHPBuilder - PHP and XML: Using the expat functions Page 4

RSS Twitter

PHP and XML: Using the expat functions - Page 4

by: Justin Grant
July 30, 2000

In the startElement and closeElement (the functions called on an open and close tag, respectively) the corresponding array is queried with the name of the tag used as the index key. If the variable exists with that keyname then the value is retrieved and appened to the 'html' property of the class for later use when we actually want to display the document contents. The characterData method simply adds the value between the tags to the html property of the class. The method that has been remarked out called PIHandler is a callback function that I have not yet implemented that will process php script straight in the original XML document if it existed.
Now, let me explain the main parser method called, you guessed it, parse() !!!
The first line calls the function xml_parser_create() that returns an instance of the expat xml parser and stores it in the class property called $this->xml_parser.
Next, to register a callback function that is a method of a class we need to use the function xml_set_object().
I use it in this way , xml_set_object($this->xml_parser, $this). I specify the class property where the xml parser is stored in the first argument, then in the second argument I specify the address of the instantiated PHP object. This let's the parser know that all the callback functions to be registered are actually methods of the class specified at that address. This is like a 'pass by reference' in c or c++ or some call it simply 'referencing a variable'.
On the next line I set an xml parser option to use case folding by calling xml_parser_set_option(). Case folding just lets the parser know that I am not concerned about case-sensitivity when I parse my XML document but you could leave it unset if you wanted to define two different tags using case sensitivity i.e. <story> or <story>. Using the function xml_set_element_handler() I specify the callback functions that I will be using for start and end tags, namely "startElement" and "endElement".
Following that I use xml_set_character_data_handler() to specify the character data handler callback function named characterData(). The remarked out function call, xml_set_processing_instruction_handler(), is the call I would use to register the function PIHandler() to deal with php code that may be included in the XML document. The remaining code just simply reads the XML file and parses it. If an error occurrs then the details are returned along with the line number that the error occured on.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Justin Grant



(Maximum characters: 1200). You have characters left.