Jump to content

Stupid XML parser


jason2

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.