xml_parser_set_option

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

xml_parser_set_option在 XML 解析器中设置选项

说明

xml_parser_set_option(XMLParser $parser, int $option, string|int|bool $value): bool

在 XML 解析器中设置选项。

参数

parser

指向要设置选项的 XML 解析器。

option

要设置的选项。见下文。

以下选项可用:

XML 解析器选项
选项常量 数据类型 说明
XML_OPTION_CASE_FOLDING bool 控制是否为此 XML 解析器启用大写转换。默认启用。
XML_OPTION_SKIP_TAGSTART integer 指定在标记名称的开头应略过多少个字符。
XML_OPTION_SKIP_WHITE bool 是否略过由空白字符组成的值。
XML_OPTION_TARGET_ENCODING string 设置要在此 XML 解析器中使用的目标编码。默认情况下,设置的编码与 xml_parser_create() 使用的源编码相同。支持的目标编码有 ISO-8859-1US-ASCIIUTF-8

value

选项的新值。

返回值

成功时返回 true,失败时返回 false

错误/异常

当传递到 option 的值无效时抛出 ValueError

在 PHP 8.0.0 之前,向 option 传递的值无效时会生成 E_WARNING 并使函数返回 false

更新日志

版本 说明
8.3.0 value 参数现在也接受 bool。选项 XML_OPTION_CASE_FOLDINGXML_OPTION_SKIP_WHITE 现在是 bool 选项。
8.0.0 parser 现在接受 XMLParser 实例;之前接受有效的 xml resource
8.0.0 如果 option 无效,现在抛出 ValueError
add a note

User Contributed Notes 3 notes

up
2
www.thomaskoch.it
15 years ago
The option XML_OPTION_SKIP_WHITE has no effect in my PHP 5.2.6 (with expat-1.95.8-5). To skip cdata composed of white space only, simply check for that at the beginning of your cdata callback function:

<?php
function callback_cdata($parser, $cdata)
{
if(!
trim($cdata))
return;

// ... continue processing ...
}
?>
up
1
pupeno at pupeno dot com
21 years ago
XML is case sensitive, then, from my point of view, disabling case folding doesn't goes against xml 1.0 specifications but the contrary, disabling case folding allow us to distiguish between diferent cases of the same letter ('a' and 'A') which of XML are two diferent things.
From my point of view, disabling case folding is a good practice and I think it should be disabled by default.
More information on:
http://www.isacat.net/2001/xml/case.htm
and
http://www.w3.org/TR/REC-xml
Thank you.
up
0
j[no_spam_please] at [thx]jessepearson dot net
17 years ago
In the function below, you need to update two lines if you don't want php to throw warnings.

change these two:
$elements[$index]['attributes'] = $tag['attributes'];
$elements[$index]['content'] = $tag['value'];

to this:
$elements[$index]['attributes'] = empty($tag['attributes']) ? "" : $tag['attributes'];
$elements[$index]['content'] = empty($tag['value']) ? "" : $tag['value'];
To Top