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;
}
}
xml parser class? Urgent!!
Moderator: General Moderators
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
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
?>