Current Location: Home> Function Categories> xml_set_element_handler

xml_set_element_handler

Setting Start and End Element Handlers
Name:xml_set_element_handler
Category:XML parser
Programming Language:php
One-line Description:Sets the start and end element handler for the XML parser.

Definition and usage

The xml_set_element_handler() function is used to specify the functions called at the beginning and end of an element in an XML document.

Note: The start and end parameters can also be an array containing object references and method names.

Example

Specifies the function to be called at the beginning and end of an element in an XML document ( note.xml ):

 <?php
$parser = xml_parser_create ( ) ;

function start ( $parser , $element_name , $element_attrs ) {
  switch ( $element_name ) {
    case "NOTE" :
      echo "NOTE<br>" ;
      break ;
    case "TO" :
      echo "To: " ;
      break ;
    case "FROM" :
      echo "From: " ;
      break ;
    case "HEADING" :
      echo "Heading: " ;
      break ;
    case "BODY" :
      echo "Message: " ;
  }
}

function stop ( $parser , $element_name ) {
  echo "<br>" ;
}

function char ( $parser , $data ) {
  echo $data ;
}

// Specify the function to be called at the beginning and end of an element in an XML document
xml_set_element_handler ( $parser , "start" , "stop" ) ;
xml_set_character_data_handler ( $parser , "char" ) ;
$fp = fopen ( "note.xml" , "r" ) ;

while ( $data = fread ( $fp , 4096 ) ) {
  xml_parse ( $parser , $data , feof ( $fp ) ) or
  die ( sprintf ( "XML error: %s on line %d" ,
  xml_error_string ( xml_get_error_code ( $parser ) ) ,
  xml_get_current_line_number ( $parser ) ) ) ;
}

xml_parser_free ( $parser ) ;
fclose ( $fp ) ;
?>

Run the instance

grammar

 xml_set_element_handler ( parser , start , end )
parameter describe
parser Required. Specify the XML parser to use
start

Required. Specifies the function to be called at the beginning of the element. The function must have three parameters:

  • $parser - Variable containing the XML parser that calls the handler
  • $name - A variable containing the name of the element that triggers this function, a string from an XML file
  • $data - an array containing element attributes from an XML file as a string
end

Required. Specifies the function to be called at the end of the element. The function must have two parameters:

  • $parser - Variable containing the XML parser that calls the handler
  • $name - A variable containing the name of the element that triggers this function, a string from an XML file
Similar Functions
Popular Articles