略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: xml_set_element_handler

2024-04-27

xml_set_element_handler

(PHP 4, PHP 5, PHP 7, PHP 8)

xml_set_element_handler建立起始和终止元素处理器

说明

xml_set_element_handler(resource $parser, callable $start_element_handler, callable $end_element_handler): bool

parser 参数指定的 XML 解析器建立元素处理器函数。参数 start_element_handlerend_element_handler 为表示函数名称的字符串,这些函数必须在为 parser 指定的解析器调用 xml_parse() 函数时已存在。

参数

parser

XML 解析器的引用,用于建立起始和终止元素处理器。

start_element_handler

start_element_handler 参数命名的函数名必须接受三个参数:

start_element_handler(resource $parser, string $name, array $attribs)
parser
第一个参数 parser 为指向要调用处理器的 XML 解析器的指针。
name
第二个参数 name 为该处理器为之被调用的元素名。如果大小写折叠(case-folding)对该解析器有效,元素名将用大写字母表示。
attribs
第三个参数 attribs 为一个包含有对应元素的属性的数组(如果该元素有属性)。数组元素的下标为属性名,元素的值即为属性的值。属性名将以和元素名同样的标准进行大小写折叠(case-folded),其值进行大小写折叠。 属性的原始顺序将会被参数保留,用 each() 函数遍历 attribs 时,该数组下表的顺序和属性的顺序相同。

注意: 除了函数名,含有对象引用的数组和方法名也可以作为参数。

end_element_handler

end_element_handler 参数命名的函数名必须接受两个参数:

end_element_handler(resource $parser, string $name)
parser
第一个参数 parser 为指向要调用处理器的 XML 解析器的指针。
name
第二个参数 name 为该处理器为之被调用的元素名。如果大小写折叠(case-folding)对该解析器有效,元素名将用大写字母表示。

如果处理器函数名被设置为空字符串或者 false,则该有问题的处理器将被屏蔽。

返回值

成功时返回 true, 或者在失败时返回 false

add a noteadd a note

User Contributed Notes 15 notes

up
2
darien at etelos dot com
15 years ago
This documentation is somewhat awry. I know it's been said many times before, but it bears repeating...

If using PHP4, you may be required to use xml_set_object() instead of calling any of the xml_set_*_handler() functions with a two-item array. It will work fine on PHP5, but move the same code to PHP4 and it will create one copie of $this (even if you use &$this) for each handler you set!

<?php
// This code will fail mysteriously on PHP4.
$this->parser = xml_parser_create();
xml_set_element_handler(
           
$this->parser,
            array(&
$this,"start_tag"),
            array(&
$this,"end_tag")
        );
       
xml_set_character_data_handler(
           
$this->parser,
            array(&
$this,"tag_data")
        );
?>

<?php
// This code will work on PHP4.
$this->parser = xml_parser_create();
xml_set_object($this->parser,&$this);
xml_set_element_handler(
           
$this->parser,
           
"start_tag",
           
"end_tag"
       
);
       
xml_set_character_data_handler(
           
$this->parser,
           
"tag_data"
       
);
?>
up
3
aw at avatartechnology dot com
17 years ago
In response to landb at mail dot net...

As the notes mention, you can pass an array that contains the reference to an object and a method name when you need... so you can call methods in your own class as handlers like this:

xml_set_element_handler($parser, array($this,"_startElement"), array($this,"_endElement"));

Hope it helps...
up
2
youniforever at naver dot com
17 years ago
<html>
  <head>
    <title>SAX Demonstration</title>
   <META HTTP-EQUIV='Content-type' CONTENT='text/html; charset=euc-kr'>
  </head>
  <body>
    <h1>RSS خ

官方地址:https://www.php.net/manual/en/function.xml-set-element-handler.php