Jump to content


Photo

Stupid XML parser


  • Please log in to reply
No replies to this topic

#1 jason2

jason2
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 10 September 2006 - 11:33 PM

I'm trying to make a really simple XML parser for my website, but its been a couple years since I've used PHP and so many things are lost in my brain now, lol. Anyway, I'm trying to achieve a simple XML parser object that you can feed it a filename, and it will spit out HTML for the content of that file. However, I'm getting the error:

Warning: xml_parse(): Unable to call handler start_tag() in C:\Program Files\Apache Group\Apache2\htdocs\parser.php on line 34

Warning: xml_parse(): Unable to call handler content() in C:\Program Files\Apache Group\Apache2\htdocs\parser.php on line 34

Warning: xml_parse(): Unable to call handler content() in C:\Program Files\Apache Group\Apache2\htdocs\parser.php on line 34

In fact, lots and lots of these errors (I'm guessing corresponding to each element it tries unsuccessfully to parse from the file. The problem is that I'm really sure I got the code right, lol. Here's the PHP code for the object, I don't understand why it can't find the functions when I named them right and all.

<?php
/************************
*  XML parser class
*************************/

class XML {
 	var $item_array = array();
	var $counter = 0;

	function start_tag($parser, $tag) {
		global $current;
		echo "Start: ".$current;
	}

	function end_tag($parser, $tag) {
	    global $current;
	    echo "Ended: ".$current;
	}

	function content($parser, $cdata) {
		global $current;
	    echo "Content: ".$cdata;
	}


	function XML($xml_file) {
		$parser = xml_parser_create();
		xml_set_element_handler($parser, "start_tag", "end_tag");
		xml_set_character_data_handler($parser, "content");

		$fp = fopen($xml_file, "r") or die("Cannot open ".$xml_file);
		$data = fread($fp, filesize($xml_file)) or die("Could not read file");

		if(!(xml_parse($parser, $data, feof($fp)))) {
			die("Error on line: ".xml_get_current_line_number($parser));
		}

		xml_parser_free($parser);
		fclose($fp);
	}



}
?>

Thanks in advance for the help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users