Jump to content

Archived

This topic is now archived and is closed to further replies.

jason2

Stupid XML parser

Recommended Posts

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:

[code]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[/code]

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.

[code]<?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);
}



}
?>[/code]

Thanks in advance for the help!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.