xml_set_object

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

xml_set_object在对象中使用 XML 解析器

说明

xml_set_object(XMLParser $parser, object $object): bool

此函数允许在 object 内部使用 parser。所有回调函数都可以用 xml_set_element_handler() 等设置,并假定为 object 的方法。

参数

parser

指向对象内要使用的 XML 解析器。

object

使用 XML 解析器的对象。

返回值

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

更新日志

版本 说明
8.0.0 parser 现在接受 XMLParser 实例;之前接受有效的 xml resource

范例

示例 #1 xml_set_object() 示例

<?php
class XMLParser
{
private
$parser;

function
__construct()
{
$this->parser = xml_parser_create();

xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->parser, "cdata");
}

function
__destruct()
{
xml_parser_free($this->parser);
unset(
$this->parser);
}

function
parse($data)
{
xml_parse($this->parser, $data);
}

function
tag_open($parser, $tag, $attributes)
{
var_dump($tag, $attributes);
}

function
cdata($parser, $cdata)
{
var_dump($cdata);
}

function
tag_close($parser, $tag)
{
var_dump($tag);
}
}

$xml_parser = new XMLParser();
$xml_parser->parse("<A ID='hallo'>PHP</A>");
?>

以上例程会输出:

string(1) "A"
array(1) {
  ["ID"]=>
  string(5) "hallo"
}
string(3) "PHP"
string(1) "A"

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top