目录

PHP xml_set_external_entity_ref_handler() 函数

❮ PHP XML 解析器参考

示例

创建 XML 解析器,设置字符数据处理程序,设置外部实体引用处理程序,并解析 XML 文档:

<?php
// Create an XML parser
$parser=xml_parser_create();

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

function ext_ent_handler($parser,$ent,$base,$sysID,$pubID)  {
echo "$ent<br>";
echo "$sysID<br>";
echo "$pubID<br>";
}

// Set the character data handler
xml_set_character_data_handler($parser,"char");

// Set the external entity reference handler
xml_set_external_entity_ref_handler($parser, "ext_ent_handler");

$fp=fopen("note_entity.xml","r");

while ($data=fread($fp,4096)) {
  // Parse XML data
  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);
fclose($fp);
?>


定义和用法

xml_set_external_entity_ref_handler() 函数指定解析器在 XML 文档中找到外部实体时要调用的函数。

笔记:处理程序参数也可以是包含对象引用和方法名称的数组。

语法

xml_set_external_entity_ref_handler( parser, handler)

参数值

Parameter Description
parser Required. Specifies the XML parser to use
handler Required. Specifies a function to be used as an event handler. The function must accept five parameters:
  • $parser - A variable containing the XML parser calling the handler
  • $name - A variable containing the name of the external entity
  • $base - The base for resolving the system identifier (system_id) of the external entity. Currently, this is always an empty string
  • $system_id - The system identifier as specified in the entity declaration
  • $public_id - The public identifier as specified in the entity declaration


技术细节

返回值: 成功则为真。失败时为 FALSE
PHP 版本: 4.0+

❮ PHP XML 解析器参考