Topic awaiting preservation: Creating an XML parsing class in PHP (Page 1 of 1) |
|
---|---|
Nervous Wreck (II) Inmate From: |
posted 01-28-2006 23:10
I'm having trouble with the below PHP class ("XMLParser") to instantiate PHP's expat parser. If I include the code below and create an object, PHP can't see the 3 XML handling functions. Does anyone see why? code: PHP Warning: xml_parse(): Unable to call handler endElement() in XMLParser.class.php
code: class XMLParser { // Container Object for PHP's XML parser var $xml_parser; // XML parser var $depth_stack; // FILO array tracking node names and depth in XML doc function XMLParser() { $this->depth_stack = array(); // Create an XML parser $this->xml_parser = xml_parser_create(); // Point to the XML Parser object for handler functions // xml_set_object($xml_parser,&$this); // Turn off case folding xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, FALSE); // Set the functions to handle opening and closing tags xml_set_element_handler($this->xml_parser, "startElement", "endElement"); // Set the function to handle blocks of character data xml_set_character_data_handler($this->xml_parser, "characterData"); } function parseFile($filename) { $data = file_get_contents($filename); xml_parse($this->xml_parser, $data) or die("XML error: \n" . xml_error_string(xml_get_error_code($this->xml_parser)) . " at line " . xml_get_current_line_number($this->xml_parser)); } function startElement($parser, $name, $attrs) { // Push node name onto stack array_push($this->depth_stack, $name); } function endElement($parser, $name) { // Pop node name from stack array_pop($this->depth_stack); } function characterData($parser, $cdata) { echo trim(htmlspecialchars($cdata)); } function destroy() { // Free up memory used by the XML parser xml_parser_free($this->xml_parser); } }
|
Paranoid (IV) Mad Scientist with Finglongers From: Germany |
posted 01-28-2006 23:17
because PHP can't pass function pointers the way you try... it usually works via 'passed function names' (sigh). |
Nervous Wreck (II) Inmate From: |
posted 01-29-2006 00:41
Thanks, Tyberius. Tried that instead, but I get a warning: code: xml_set_element_handler( $this->xml_parser, array($this,'startElement'), array($this,'endElement'));
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 01-29-2006 10:07
yeah, you replace the function names. My bad - it was late last night ;-) |