Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

The xml_set_object function associates an object with a parser. You must pass the
parser identifier and a reference to an object. This is best done within the object using the
this variable. After using this function, PHP will call methods of the object instead of
the functions in the global scope when you name handlers.


<?


class myParser
{
var $parser;


function parse($filename)
{
//create parser
if(!($this->parser =
xml_parser_create()))
{
print("Could not create
parser!
\n");
exit();
}


//associate parser with this object
xml_set_object($this->parser, &$this);


//register handlers
xml_set_character_data_handler($this-



parser,
"cdataHandler");
xml_set_element_handler($this->parser,
"startHandler", "endHandler");



/*
* Parse file
/
if(!($fp = fopen($filename, "r")))
{
print("Couldn't open
example.xml!
\n");
xml_parser_free($this->parser);
return;
}


while($line = fread($fp, 1024))
{
xml_parse($this->parser, $line,
feof($fp));

Free download pdf