Closed Thread Icon

Topic awaiting preservation: Creating an XML parsing class in PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27405" title="Pages that link to Topic awaiting preservation: Creating an XML parsing class in PHP (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Creating an XML parsing class in PHP <span class="small">(Page 1 of 1)</span>\

 
divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted 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



If I move them outside the class, PHP can then see them, but the functions now can't see inside the XMLParser object scope to manipulate $depth_stack. I want the functions to remain inside XMLParser.

I've tried Kevin Yank's "PHP and XML" OOP solution here (http://www.sitepoint.com/article/php-xml-parsing-rss-1-0), using a pass-by-reference -- xml_set_object($xml_parser,&$this) -- but pass-by-reference in PHP4 is now deprecated, not to mention it still doesn't work. I've commented it out below but left it in to show where and how I've tried it. I could create class inside XMLParser (e.g. class DataHandler), problem is I would still have to pass-by-reference to $xml_parser.

Any help would be appreciated. I'm trying to not make this reliant on additional PHP libraries, which is why I haven't yet switched to a DOM API or XSLT solution.

Thanks,
DC

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);
	}
}



(Edited by divinechaos on 01-28-2006 23:12)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted 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).

try
xml_set_element_handler( array($this,'xml_parser'), "startElement", "endElement")
instead.

so long,

->Tyberius Prime

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 01-29-2006 00:41

Thanks, Tyberius. Tried that instead, but I get a warning:
PHP Warning: xml_set_element_handler(): supplied argument is not a valid XML Parser resource

Do I instead want to replace the function names with arrays?

code:
xml_set_element_handler( $this->xml_parser, array($this,'startElement'), array($this,'endElement'));



I Googled "php passed function names" but didn't find much as a result, so I'm not sure quite what is wrong or where to consult, and nothing on php.net.

Thanks for the prompt response.

Cheers,
DC

Edit: Missed a single-quote.

(Edited by divinechaos on 01-29-2006 00:42)

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 01-29-2006 10:07

yeah, you replace the function names. My bad - it was late last night ;-)

« BackwardsOnwards »

Show Forum Drop Down Menu