Set a function to be called when the parser an unparsed entity in the XML document in PHP

Description

The following code shows how to set a function to be called when the parser an unparsed entity in the XML document.

Example


//from w ww.  j  av a  2  s.  c  o m
<?php $parser=xml_parser_create();

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

function unparsed_ent_handler($parser,$entname,$base,$sysID,$pubID,$notname){
      print "$entname<br />";
      print "$sysID<br />";
      print "$pubID<br />";
      print "$notname<br />";
}

xml_set_character_data_handler($parser,"char");
xml_set_unparsed_entity_decl_handler($parser,"unparsed_ent_handler");

$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);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser