ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
该扩展使用Expat XML解析器。 Expat是一种基于事件的解析器,它把XML文档视为一系列事件。当某个事件发生时,它调用一个指定的函数处理它。 Expat是无验证的解析器,忽略任何链接到文档的DTD。但是,如果文档的形式不好,则会以一个错误消息结束。 由于它是一种基于事件,并且无验证的解析器,因此具有快速并适合Web应用程序的特性。 XML解析器函数允许您创建XML解析器,并为XML事件定义句柄。 它支持 PHP 所提供的 3 种[字符编码](https://www.php.net/manual/zh/xml.encoding.php):*US-ASCII*,*ISO-8859-1*和*UTF-8*。 不支持*UTF-16*。 此扩展可[创建 XML 解析器](https://www.php.net/manual/zh/function.xml-parser-create.php)并为不同的 XML 事件定义*处理程序(handler)*。 每个 XML 解析器还存在少数可以调节的[参数](https://www.php.net/manual/zh/function.xml-parser-set-option.php)。 需求: 此扩展需要[libxml](https://www.php.net/manual/zh/book.libxml.php)PHP 扩展 默认情况下,此扩展使用expat compat layer。也可使用expat, 此库位于[» http://www.jclark.com/xml/expat.html](http://www.jclark.com/xml/expat.html)Expat是一种基于事件的解析器 安装: 这些函数默认为有效的,使用了捆绑的 expat 库。您可以通过参数 **--disable-xml** 来屏蔽 XML 的支持。如果您将 PHP 编译为 Apache 1.3.9 或更高版本的一个模块, PHP 将自动使用 Apache 捆绑的expat库。如果您不希望使用该捆绑的 expat 库,请在运行 PHP 的 configure 配置脚本时使用参数 **--with-expat-dir=DIR** ,其中 DIR 应该指向 expat 安装的根目录。 PHP 的 Windows 版本已内建对此扩展的支持。不需要载入额外的扩展来使用这些函数 ## 解析器函数 * [utf8\_decode](https://www.php.net/manual/zh/function.utf8-decode.php)— 将用 UTF-8 方式编码的 ISO-8859-1 字符串转换成单字节的 ISO-8859-1 字符串。 * [utf8\_encode](https://www.php.net/manual/zh/function.utf8-encode.php)— 将 ISO-8859-1 编码的字符串转换为 UTF-8 编码 * [xml\_error\_string](https://www.php.net/manual/zh/function.xml-error-string.php)— 获取 XML 解析器的错误字符串 * [xml\_get\_current\_byte\_index](https://www.php.net/manual/zh/function.xml-get-current-byte-index.php)— 获取 XML 解析器的当前字节索引 * [xml\_get\_current\_column\_number](https://www.php.net/manual/zh/function.xml-get-current-column-number.php)— 获取 XML 解析器的当前列号 * [xml\_get\_current\_line\_number](https://www.php.net/manual/zh/function.xml-get-current-line-number.php)— 获取 XML 解析器的当前行号 * [xml\_get\_error\_code](https://www.php.net/manual/zh/function.xml-get-error-code.php)— 获取 XML 解析器错误代码 * [xml\_parse\_into\_struct](https://www.php.net/manual/zh/function.xml-parse-into-struct.php)— 将 XML 数据解析到数组中 ~~~ $simple = '<para><note>simple note</note></para>'; $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index); xml_parser_free($p); echo "Index array\n"; print_r($index); echo "\nVals array\n"; print_r($vals); ~~~ 输出: ~~~ Index array Array ( [PARA] => Array ( [0] => 0 [1] => 2 ) [NOTE] => Array ( [0] => 1 ) ) Vals array Array ( [0] => Array ( [tag] => PARA [type] => open [level] => 1 ) [1] => Array ( [tag] => NOTE [type] => complete [level] => 2 [value] => simple note ) [2] => Array ( [tag] => PARA [type] => close [level] => 1 ) ) ~~~ * [xml\_parse](https://www.php.net/manual/zh/function.xml-parse.php)— 开始解析一个 XML 文档 * [xml\_parser\_create\_ns](https://www.php.net/manual/zh/function.xml-parser-create-ns.php)— 生成一个支持命名空间的 XML 解析器 * [xml\_parser\_create](https://www.php.net/manual/zh/function.xml-parser-create.php)— 建立一个 XML 解析器 * [xml\_parser\_free](https://www.php.net/manual/zh/function.xml-parser-free.php)— 释放指定的 XML 解析器 * [xml\_parser\_get\_option](https://www.php.net/manual/zh/function.xml-parser-get-option.php)— 从 XML 解析器获取选项设置信息 * [xml\_parser\_set\_option](https://www.php.net/manual/zh/function.xml-parser-set-option.php)— 为指定 XML 解析进行选项设置 * [xml\_set\_character\_data\_handler](https://www.php.net/manual/zh/function.xml-set-character-data-handler.php)— 建立字符数据处理器 * [xml\_set\_default\_handler](https://www.php.net/manual/zh/function.xml-set-default-handler.php)— 建立默认处理器 * [xml\_set\_element\_handler](https://www.php.net/manual/zh/function.xml-set-element-handler.php)— 建立起始和终止元素处理器 * [xml\_set\_end\_namespace\_decl\_handler](https://www.php.net/manual/zh/function.xml-set-end-namespace-decl-handler.php)— 建立终止命名空间声明处理器 * [xml\_set\_external\_entity\_ref\_handler](https://www.php.net/manual/zh/function.xml-set-external-entity-ref-handler.php)— 建立外部实体指向处理器 * [xml\_set\_notation\_decl\_handler](https://www.php.net/manual/zh/function.xml-set-notation-decl-handler.php)— 建立注释声明处理器 * [xml\_set\_object](https://www.php.net/manual/zh/function.xml-set-object.php)— 在对象中使用 XML 解析器 * [xml\_set\_processing\_instruction\_handler](https://www.php.net/manual/zh/function.xml-set-processing-instruction-handler.php)— 建立处理指令(PI)处理器 * [xml\_set\_start\_namespace\_decl\_handler](https://www.php.net/manual/zh/function.xml-set-start-namespace-decl-handler.php)— 建立起始命名空间声明处理器 * [xml\_set\_unparsed\_entity\_decl\_handler](https://www.php.net/manual/zh/function.xml-set-unparsed-entity-decl-handler.php)— 建立未解析实体定义声明处理器