xml parser class? Urgent!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

xml parser class? Urgent!!

Post by toshesh »

Dear php fellows

I'm trying to create a xml parser class with the following functions but I get errors saying "unable to call handler startElement()" and "unable to call handler endElement()" and "unable to call handler characterData()".
I've been working on this for ages and can't figure out why so could anyone show some light on this? I need to be able to "see" the data coming in from characterData in the endElement too. An xml string is sent to the constructor.

class xmlread {
function xmlread22($str) {
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler ($xml_parser, "characterData");
xml_parse($xml_parser, $str);
xml_parser_free($xml_parser);
}

function startElement($parser, $name, $attrs) {
global $ctag;
$this->ctag = $name;
}

function endElement($parser, $name) {
do something;
}
}
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Please use the PHP or CODE bbTags when posting code like that..

You should use an instance/global variable and not a function/local one for the parsing object, then you must tell PHP to refer to this object, something like

Code: Select all

<?php
 $this->parser = xml_parser_create();
 xml_set_object($this->parser,&$this);
 xml_set_element_handler($this->parser,"startElement","endElement");

 // and ofcourse those functions must be methods of the same class
?>
Post Reply