当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > xml_set_Element_handler() 函数

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 xml_set_Element_handler() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 447 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The xml_set_element_handler() function specifies functions to be called at the start and end of an element in the XML document.
xml_set_element_handler()函数的作用是:设置处理XML文档中元素的开始和结束的函数。

This function returns TRUE on success, or FALSE on failure.
如果函数执行成功,将返回True;如果执行失败,将返回False。

Syntax
语法

xml_set_element_handler(parser,start,end)

Parameter参数 Description描述
parser Required. Specifies XML parser to use
必要参数。指定需要使用的XML解析器
start Required. Specifies a function to be called at the start of an element
必要参数。指定需要处理的元素的起始位置
end Required. Specifies a function to be called at the end of an element
必要参数。指定需要处理的元素的终止位置

The Function specified by the "start" parameter must have three parameters:
这个函数所指定的“start”参数必须包含下面三个参数:

Parameter参数 Description描述
parser Required. Specifies a variable containing the XML parser calling the handler
必要参数。指定一个包含XML解析器的可用值
name Required. Specifies a variable containing the name of the elements, that triggers this function, from the XML file as a string
必要参数。指定一个包含元素名称的变量,它以数组的形式激发这个函数
data Required. Specifies an array containing the elements attributes from the XML file as a string
必要参数。指定包含元素属性的一个数组

The Function specified by the "end" parameter must have two parameters:
这个函数所指定的“end”参数必须包含下面两个参数:

Parameter参数 Description描述
parser Required. Specifies a variable containing the XML parser calling the handler
必要参数。指定一个包含XML解析器的可用值
name Required. Specifies a variable containing the name of the elements, that triggers this function, from the XML file as a string
必要参数。指定一个包含元素名称的变量,它以数组的形式激发这个函数


Tips and Notes
注意点

Note: The start and end parameters can also be an array containing an object reference and a method name.
注意:start和end参数可以是一个包含对象参数和方法名称的数组。


Example
案例

<?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; }
xml_set_element_handler($parser,"start","stop");
xml_set_character_data_handler($parser,"char");
$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 output of the code above will be:
上述代码将输出下面的结果:

-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!

评论 (0) All

登陆 | 还没注册?