Home » » xml_set_element_handler

xml_set_element_handler

Written By 1 on Tuesday, October 2, 2012 | 9:18 AM

bool xml_set_element_handler ( resource $parser , callable $start_element_handler , callable $end_element_handler )

Set up start and end element handlers

Sets the element handler functions for the XML parser. start_element_handler and end_element_handler are strings containing the names of functions that must exist when xml_parse() is called for parser.


This documentation is somewhat awry. I know it's been said many times before, but it bears repeating...



If using PHP4, you may be required to use xml_set_object() instead of calling any of the xml_set_*_handler() functions with a two-item array. It will work fine on PHP5, but move the same code to PHP4 and it will create one copie of $this (even if you use &$this) for each handler you set!



<?php

// This code will fail mysteriously on PHP4.

$this->parser = xml_parser_create();

xml_set_element_handler(

           
$this->parser,

            array(&
$this,"start_tag"),

            array(&
$this,"end_tag")

        );

       
xml_set_character_data_handler(

           
$this->parser,

            array(&
$this,"tag_data")

        );

?>



<?php

// This code will work on PHP4.

$this->parser = xml_parser_create();

xml_set_object($this->parser,&$this);

xml_set_element_handler(

           
$this->parser,

           
"start_tag",

           
"end_tag"

       
);

       
xml_set_character_data_handler(

           
$this->parser,

           
"tag_data"

       
);

?>


0 Comment:

Post a Comment