PHP xml_set_default_handler() Function

In this chapter you will learn:

  1. Description for PHP xml_set_default_handler() Function
  2. Syntax for PHP xml_set_default_handler() Function
  3. Parameter for PHP xml_set_default_handler() Function
  4. Return for PHP xml_set_default_handler() Function
  5. Example - Set the default data handler for the XML parser

Description

The xml_set_default_handler() function sets the default data handler for the XML parser.

This function specifies what function to be called whenever the parser finds data in the XML file.

Syntax

PHP xml_set_default_handler() Function has the following syntax.

xml_set_default_handler(parser,handler)

Parameter

ParameterIs RequiredDescription
parserRequired.XML parser to use
handlerRequired.A function to be used as an event handler

The Function specified by the "handler" parameter must have two parameters:

Parameteris RequiredDescription
parserRequired.A variable containing the XML parser calling the handler
dataRequired.A variable containing the data from the XML file as a string

The data_handler parameter can be an array containing an object reference and a method name.

Return

This function returns TRUE on success, or FALSE on failure.

Example

XML File


<?xml version="1.0" encoding="ISO-8859-1"?>
<books>/*j  av a  2  s.  com*/
  <name>PHP</name>
  <name>Java</name>
</books>

PHP Code


<?php/*from   ja v a2 s.co m*/
$parser=xml_parser_create();

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

xml_set_default_handler($parser,'defaultH');
$fp=fopen("test.xml","r");

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

xml_parser_free($parser);
?>

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP xml_set_element_handler() Function
  2. Syntax for PHP xml_set_element_handler() Function
  3. Parameter for PHP xml_set_element_handler() Function
  4. Return for PHP xml_set_element_handler() Function
  5. Example - specifies functions to be called at the start and end of an element in the XML document