Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 12293

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>XML 语法解析函数</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="函数参考"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="xdiff_string_patch"
HREF="function.xdiff-string-patch.html"><LINK
REL="NEXT"
TITLE="utf8_decode"
HREF="function.utf8-decode.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="reference"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.xdiff-string-patch.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.utf8-decode.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.xml"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>CLXXIX. XML 语法解析函数</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN240377"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.intro"
>简介</A
></H1
><P
>&#13;    XML(eXtensible Markup Language,可扩展标记语言)是一种在 web
    上进行文档交换的数据格式。该语言是由 W3C(World
    Wide Web Concortium,世界万维网组织)定义的一种标准。可以访问
    <A
HREF="http://www.w3.org/XML/"
TARGET="_top"
>http://www.w3.org/XML/</A
> 以获取关于 XML 及其相关技术的更多信息。
   </P
><P
>&#13;    本扩展模块可为 James Clark 的 <SPAN
CLASS="productname"
>expat</SPAN
>
    提供支持。该工具包帮助解析 XML 文档(而非 XML
    文档的有效化)。它支持三种源代码的<A
HREF="ref.xml.html#xml.encoding"
>编码方式</A
>,这三种编码方式也被 PHP
    本身所支持,它们分别是:<TT
CLASS="literal"
>US-ASCII</TT
>、<TT
CLASS="literal"
>ISO-8859-1</TT
>
    和 <TT
CLASS="literal"
>UTF-8</TT
>。本系统尚不支持 <TT
CLASS="literal"
>UTF-16</TT
>。
   </P
><P
>&#13;    本扩展模块使用户能够<A
HREF="function.xml-parser-create.html"
>建立
    XML 语法解析器</A
>,并对不同的 XML 事件定义对应的处理器。每个
    XML 语法解析器都有若干个可根据需要调整的<A
HREF="function.xml-parser-set-option.html"
>参数</A
>。
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.requirements"
>需求</A
></H1
><P
>&#13;    本扩展模块默认使用 <SPAN
CLASS="productname"
>expat compat layer</SPAN
>。它也可以使用
    <SPAN
CLASS="productname"
>expat</SPAN
>,可以访问
    <A
HREF="http://www.jclark.com/xml/expat.html"
TARGET="_top"
>http://www.jclark.com/xml/expat.html</A
> 来获取。expat
    自带的 Makefile 文件不会生成默认的扩展库,可以使用以下的生成规则来实现:
    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="makefile"
>libexpat.a: $(OBJS)
    ar -rc $@ $(OBJS)
    ranlib $@</PRE
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    请访问 <A
HREF="http://sourceforge.net/projects/expat/"
TARGET="_top"
>http://sourceforge.net/projects/expat/</A
> 以获取
    expat 源文件的 RPM 包。
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.installation"
>安装</A
></H1
><P
>&#13;  这些函数默认为有效的,它们使用了捆绑的 expat 库。您可以通过参数 <CODE
CLASS="option"
>--disable-xml</CODE
> 来屏蔽 XML 的支持。如果您将 PHP 编译为 Apache 1.3.9 或更高版本的一个模块, PHP 将自动使用 Apache 捆绑的 <SPAN
CLASS="productname"
>expat</SPAN
> 库。如果您不希望使用该捆绑的 expat 库,请在运行 PHP 的 configure 配置脚本时使用参数 <CODE
CLASS="option"
>--with-expat-dir=DIR</CODE
>,其中 DIR 应该指向 expat 安装的根目录。
 </P
><P
> <TT
CLASS="literal"
>PHP</TT
> 的 Windows
版本已经内置该扩展模块的支持。无需加载任何附加扩展库即可使用这些函数。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.configuration"
>运行时配置</A
></H1
><P
>本扩展模块在 <TT
CLASS="filename"
>php.ini</TT
> 中未定义任何配置选项。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.resources"
>资源类型</A
></H1
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="xml.resources.xml"
><TT
CLASS="literal"
>xml</TT
></A
></H2
><P
>&#13;     由 <A
HREF="function.xml-parser-create.html"
><B
CLASS="function"
>xml_parser_create()</B
></A
> 和
     <A
HREF="function.xml-parser-create-ns.html"
><B
CLASS="function"
>xml_parser_create_ns()</B
></A
>
     返回的 <TT
CLASS="literal"
>xml</TT
> 资源引用了一个 XML
     解析器实例,将被用在本扩展库提供的函数中。
    </P
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.constants"
>预定义常量</A
></H1
><P
>以下常量由本扩展模块定义,因此只有在本扩展模块被编译到
PHP 中,或者在运行时被动态加载后才有效。</P
><P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_NONE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_NO_MEMORY</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_SYNTAX</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_NO_ELEMENTS</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_INVALID_TOKEN</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_UNCLOSED_TOKEN</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_PARTIAL_CHAR</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_TAG_MISMATCH</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_DUPLICATE_ATTRIBUTE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_JUNK_AFTER_DOC_ELEMENT</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_PARAM_ENTITY_REF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_UNDEFINED_ENTITY</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_RECURSIVE_ENTITY_REF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_ASYNC_ENTITY</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_BAD_CHAR_REF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_BINARY_ENTITY_REF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_MISPLACED_XML_PI</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_UNKNOWN_ENCODING</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_INCORRECT_ENCODING</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_UNCLOSED_CDATA_SECTION</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_ERROR_EXTERNAL_ENTITY_HANDLING</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_OPTION_CASE_FOLDING</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_OPTION_TARGET_ENCODING</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_OPTION_SKIP_TAGSTART</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>XML_OPTION_SKIP_WHITE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.eventhandlers"
>事件处理器</A
></H1
><P
>&#13;    XML 事件处理器定义如下:
    <DIV
CLASS="table"
><A
NAME="AEN240585"
></A
><P
><B
>表 1. 已支持的 XML 事件处理器</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>用来设置处理器的 PHP 函数</TH
><TH
>事件描述</TH
></TR
></THEAD
><TBODY
><TR
><TD
><A
HREF="function.xml-set-element-handler.html"
><B
CLASS="function"
>xml_set_element_handler()</B
></A
></TD
><TD
>&#13;         元素事件(Element events)将在 XML
         解析器遇到标记符的起始符或者终止符时发生。另外,对于起始符和终止符也有独立的处理器。
        </TD
></TR
><TR
><TD
>&#13;         <A
HREF="function.xml-set-character-data-handler.html"
><B
CLASS="function"
>xml_set_character_data_handler()</B
></A
>
        </TD
><TD
>&#13;         粗略的说,字符数据(Character data)是指 XML
         文档中所有标记符以外的内容,包括标记符之间的空格。需要注意的是
         XML 语法解析器不会加上或者去掉任何空格。空格的取舍将由应用程序(也就是你自己)来决定。
        </TD
></TR
><TR
><TD
>&#13;         <A
HREF="function.xml-set-processing-instruction-handler.html"
><B
CLASS="function"
>xml_set_processing_instruction_handler()</B
></A
>
        </TD
><TD
>&#13;         PHP 程序员对“处理指令”(Processing
         Instructions,PI)应该已经很熟悉了。&#60;?php ?&#62;
         就是一个处理指令,其中 <TT
CLASS="replaceable"
><I
>php</I
></TT
>
         被称为“PI target”。除了以“XML”开头的 PI target
         已被保留以外,对这些 PI 的处理将由应用程序来完成。
        </TD
></TR
><TR
><TD
><A
HREF="function.xml-set-default-handler.html"
><B
CLASS="function"
>xml_set_default_handler()</B
></A
></TD
><TD
>&#13;         所有无法被其它处理器处理的事件将由默认处理器来处理。这些事件包括诸如
         XML 和文档类型声明等内容。
        </TD
></TR
><TR
><TD
>&#13;         <A
HREF="function.xml-set-unparsed-entity-decl-handler.html"
><B
CLASS="function"
>xml_set_unparsed_entity_decl_handler()</B
></A
>
        </TD
><TD
>&#13;         该处理器将在遇到无法解析的实体名称(NDATA)声明时被调用。
        </TD
></TR
><TR
><TD
>&#13;         <A
HREF="function.xml-set-notation-decl-handler.html"
><B
CLASS="function"
>xml_set_notation_decl_handler()</B
></A
>
        </TD
><TD
>&#13;         该处理器将在声明一个注释时被调用。
        </TD
></TR
><TR
><TD
>&#13;         <A
HREF="function.xml-set-external-entity-ref-handler.html"
><B
CLASS="function"
>xml_set_external_entity_ref_handler()</B
></A
>
        </TD
><TD
>&#13;         当 XML 解析器遇到指向外部解析的一般实体名时,该处理器将被调用。该指向的目标可以是一个文件,也可以是
         URL。请参阅“<A
HREF="ref.xml.html#example.xml-external-entity"
>外部实体名范例</A
>”。
        </TD
></TR
></TBODY
></TABLE
></DIV
>
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.case-folding"
>大小写折叠(Case Folding)</A
></H1
><P
>&#13;    元素处理函数可能会导致元素名称“大小写折叠”(<I
CLASS="glossterm"
>case-folded</I
>)。“大小写折叠”被 XML 标准定义为“一个应用于一系列字符的过程,在该过程中,这些字符中的所有的非大写字符将被替换成它们对应大写等价字符”。换句话说,对于 XML,“大小写折叠”就是指将字符串转换成大写字符。
   </P
><P
>&#13;    所有被传递给处理器函数的元素名称将默认的发生“大小写折叠”。该过程可以分别被
    <A
HREF="function.xml-parser-get-option.html"
><B
CLASS="function"
>xml_parser_get_option()</B
></A
> 和
    <A
HREF="function.xml-parser-set-option.html"
><B
CLASS="function"
>xml_parser_set_option()</B
></A
> 函数查询和控制。
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.error-codes"
>错误代码</A
></H1
><P
>&#13;    以下常量被定义为 XML 的错误代码,将由 <A
HREF="function.xml-parse.html"
><B
CLASS="function"
>xml_parse()</B
></A
> 返回:
    <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
>XML_ERROR_NONE</TD
></TR
><TR
><TD
>XML_ERROR_NO_MEMORY</TD
></TR
><TR
><TD
>XML_ERROR_SYNTAX</TD
></TR
><TR
><TD
>XML_ERROR_NO_ELEMENTS</TD
></TR
><TR
><TD
>XML_ERROR_INVALID_TOKEN</TD
></TR
><TR
><TD
>XML_ERROR_UNCLOSED_TOKEN</TD
></TR
><TR
><TD
>XML_ERROR_PARTIAL_CHAR</TD
></TR
><TR
><TD
>XML_ERROR_TAG_MISMATCH</TD
></TR
><TR
><TD
>XML_ERROR_DUPLICATE_ATTRIBUTE</TD
></TR
><TR
><TD
>XML_ERROR_JUNK_AFTER_DOC_ELEMENT</TD
></TR
><TR
><TD
>XML_ERROR_PARAM_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_UNDEFINED_ENTITY</TD
></TR
><TR
><TD
>XML_ERROR_RECURSIVE_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_ASYNC_ENTITY</TD
></TR
><TR
><TD
>XML_ERROR_BAD_CHAR_REF</TD
></TR
><TR
><TD
>XML_ERROR_BINARY_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF</TD
></TR
><TR
><TD
>XML_ERROR_MISPLACED_XML_PI</TD
></TR
><TR
><TD
>XML_ERROR_UNKNOWN_ENCODING</TD
></TR
><TR
><TD
>XML_ERROR_INCORRECT_ENCODING</TD
></TR
><TR
><TD
>XML_ERROR_UNCLOSED_CDATA_SECTION</TD
></TR
><TR
><TD
>XML_ERROR_EXTERNAL_ENTITY_HANDLING</TD
></TR
></TBODY
></TABLE
><P
></P
>
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.encoding"
>字符编码</A
></H1
><P
>&#13;    PHP 的 XML 扩展库支持不同字符编码(<I
CLASS="glossterm"
>character encoding</I
>)的
    <A
HREF="http://www.unicode.org/"
TARGET="_top"
>Unicode</A
>
    字符集。字符编码有两种形式,它们分别是“源编码”(<I
CLASS="glossterm"
>source
    encoding</I
>)和“目标编码”(<I
CLASS="glossterm"
>target
    encoding</I
>)。PHP 对文档内部表示的编码方式是
    <TT
CLASS="literal"
>UTF-8</TT
>。
   </P
><P
>&#13;    源编码将在 XML 文档被<A
HREF="function.xml-parse.html"
>解析</A
>后完成。源编码可在<A
HREF="function.xml-parser-create.html"
>建立一个 XML
    解析器</A
>时指明(该编码方式在 XML
    解析器的生命周期中不能被再次改变)。支持的编码方式包括
    <TT
CLASS="literal"
>ISO-8859-1</TT
>,<TT
CLASS="literal"
>US-ASCII</TT
>
    和 <TT
CLASS="literal"
>UTF-8</TT
>。前两种为单字节编码,即每个字符被一个单一的字节表示。<TT
CLASS="literal"
>UTF-8</TT
>
    支持 1 至 4 个字节的多 bit(最多 12)字符编码。PHP 默认使用
    <TT
CLASS="literal"
>ISO-8859-1</TT
> 作为源编码方式。
   </P
><P
>&#13;    目标编码将在 PHP 向 XML 处理器函数传送数据时被完成。当 XML
    解析器被建立后,目标编码将被设置成与源编码相同的编码方式,但该方式可在任何时候被更改。目标编码将影响字符数据、标记符名称以及处理指令目标(PI target)。
   </P
><P
>&#13;    如果 XML 解析器遇到其源编码方式表示能力之外的字符,它将返回一个错误。
   </P
><P
>&#13;    当 PHP 在被解析的 XML 文档中遇到当前目标编码无法表示的字符时,这些字符将被“降级”。简单的说,这些字符将被问号替换。
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="xml.examples"
>范例</A
></H1
><P
>&#13;    以下是 PHP 脚本解析 XML 文档的一些范例。
   </P
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="example.xml-structure"
>XML 元素结构范例</A
></H2
><P
>&#13;     第一个范例用缩进格式显示一个文档中起始元素的结构。
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN240682"
></A
><P
><B
>例 1. 显示 XML 元素结构</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$file </font><font color="#007700">= </font><font color="#DD0000">"data.xml"</font><font color="#007700">;<br /></font><font color="#0000BB">$depth </font><font color="#007700">= array();<br /><br />function </font><font color="#0000BB">startElement</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$attrs</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global </font><font color="#0000BB">$depth</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;for (</font><font color="#0000BB">$i </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$i </font><font color="#007700">&lt; </font><font color="#0000BB">$depth</font><font color="#007700">[</font><font color="#0000BB">$parser</font><font color="#007700">]; </font><font color="#0000BB">$i</font><font color="#007700">++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&nbsp;&nbsp;"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"$name</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$depth</font><font color="#007700">[</font><font color="#0000BB">$parser</font><font color="#007700">]++;<br />}<br /><br />function </font><font color="#0000BB">endElement</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$name</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global </font><font color="#0000BB">$depth</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$depth</font><font color="#007700">[</font><font color="#0000BB">$parser</font><font color="#007700">]--;<br />}<br /><br /></font><font color="#0000BB">$xml_parser </font><font color="#007700">= </font><font color="#0000BB">xml_parser_create</font><font color="#007700">();<br /></font><font color="#0000BB">xml_set_element_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"startElement"</font><font color="#007700">, </font><font color="#DD0000">"endElement"</font><font color="#007700">);<br />if (!(</font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">"could not open XML input"</font><font color="#007700">);<br />}<br /><br />while (</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">4096</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">xml_parse</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"XML error: %s at line %d"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_error_string</font><font color="#007700">(</font><font color="#0000BB">xml_get_error_code</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_get_current_line_number</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">)));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="example.xml-map-tags"
>XML 标记符映射范例</A
></H2
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN240688"
></A
><P
><B
>例 2. 将 XML 映射为 HTML</B
></P
><P
>&#13;       以下范例将 XML 文档中的标记符直接映射成 HTML
       标记符。在“映射数组”中不存在的元素将被忽略。当然,该范例将只对一个特定的
       XML 文档有效。
      </P
><P
>&#13;       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$file </font><font color="#007700">= </font><font color="#DD0000">"data.xml"</font><font color="#007700">;<br /></font><font color="#0000BB">$map_array </font><font color="#007700">= array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"BOLD"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#DD0000">"B"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"EMPHASIS" </font><font color="#007700">=&gt; </font><font color="#DD0000">"I"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"LITERAL"&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#DD0000">"TT"<br /></font><font color="#007700">);<br /><br />function </font><font color="#0000BB">startElement</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$attrs</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global </font><font color="#0000BB">$map_array</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if (isset(</font><font color="#0000BB">$map_array</font><font color="#007700">[</font><font color="#0000BB">$name</font><font color="#007700">])) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;$map_array</font><font color="#007700">[</font><font color="#DD0000">$name</font><font color="#007700">]</font><font color="#DD0000">&gt;"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">endElement</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$name</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global </font><font color="#0000BB">$map_array</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if (isset(</font><font color="#0000BB">$map_array</font><font color="#007700">[</font><font color="#0000BB">$name</font><font color="#007700">])) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;/$map_array</font><font color="#007700">[</font><font color="#DD0000">$name</font><font color="#007700">]</font><font color="#DD0000">&gt;"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">characterData</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$data</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$xml_parser </font><font color="#007700">= </font><font color="#0000BB">xml_parser_create</font><font color="#007700">();<br /></font><font color="#FF8000">// 使用大小写折叠来保证我们能在元素数组中找到这些元素名称<br /></font><font color="#0000BB">xml_parser_set_option</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">XML_OPTION_CASE_FOLDING</font><font color="#007700">, </font><font color="#0000BB">true</font><font color="#007700">);<br /></font><font color="#0000BB">xml_set_element_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"startElement"</font><font color="#007700">, </font><font color="#DD0000">"endElement"</font><font color="#007700">);<br /></font><font color="#0000BB">xml_set_character_data_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"characterData"</font><font color="#007700">);<br />if (!(</font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">"could not open XML input"</font><font color="#007700">);<br />}<br /><br />while (</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">4096</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">xml_parse</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"XML error: %s at line %d"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_error_string</font><font color="#007700">(</font><font color="#0000BB">xml_get_error_code</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_get_current_line_number</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">)));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
>
      </P
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
><DIV
CLASS="section"
><H2
CLASS="section"
><A
NAME="example.xml-external-entity"
>XML 外部实体范例</A
></H2
><P
>&#13;     该范例能够高亮显示 XML 源代码。它将说明如何外部实体指向处理器来包含和解析其它文档,如何处理 PIs,以及一种确定包含有 PIs 的代码的可信度。
    </P
><P
>&#13;     能被该范例使用的的 XML 文档(<TT
CLASS="filename"
>xmltest.xml</TT
> 和
     <TT
CLASS="filename"
>xmltest2.xml</TT
>)被列在该范例之后。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN240700"
></A
><P
><B
>例 3. 外部实体范例</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$file </font><font color="#007700">= </font><font color="#DD0000">"xmltest.xml"</font><font color="#007700">;<br /><br />function </font><font color="#0000BB">trustedFile</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// only trust local files owned by ourselves<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">if (!</font><font color="#0000BB">eregi</font><font color="#007700">(</font><font color="#DD0000">"^([a-z]+)://"</font><font color="#007700">, </font><font color="#0000BB">$file</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp; </font><font color="#0000BB">fileowner</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">) == </font><font color="#0000BB">getmyuid</font><font color="#007700">()) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">startElement</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$attribs</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&amp;lt;&lt;font color=</font><font color="#007700">\"</font><font color="#DD0000">#0000cc</font><font color="#007700">\"</font><font color="#DD0000">&gt;$name&lt;/font&gt;"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$attribs</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">$attribs </font><font color="#007700">as </font><font color="#0000BB">$k </font><font color="#007700">=&gt; </font><font color="#0000BB">$v</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">" &lt;font color=</font><font color="#007700">\"</font><font color="#DD0000">#009900</font><font color="#007700">\"</font><font color="#DD0000">&gt;$k&lt;/font&gt;=</font><font color="#007700">\"</font><font color="#DD0000">&lt;font<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color=</font><font color="#007700">\"</font><font color="#DD0000">#990000</font><font color="#007700">\"</font><font color="#DD0000">&gt;$v&lt;/font&gt;</font><font color="#007700">\"</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&amp;gt;"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">endElement</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$name</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&amp;lt;/&lt;font color=</font><font color="#007700">\"</font><font color="#DD0000">#0000cc</font><font color="#007700">\"</font><font color="#DD0000">&gt;$name&lt;/font&gt;&amp;gt;"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">characterData</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;b&gt;$data&lt;/b&gt;"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">PIHandler</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$target</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;switch (</font><font color="#0000BB">strtolower</font><font color="#007700">(</font><font color="#0000BB">$target</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#DD0000">"php"</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;global </font><font color="#0000BB">$parser_file</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// If the parsed document is "trusted", we say it is safe<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// to execute PHP code inside it.&nbsp;&nbsp;If not, display the code<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// instead.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">if (</font><font color="#0000BB">trustedFile</font><font color="#007700">(</font><font color="#0000BB">$parser_file</font><font color="#007700">[</font><font color="#0000BB">$parser</font><font color="#007700">])) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval(</font><font color="#0000BB">$data</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"Untrusted PHP code: &lt;i&gt;%s&lt;/i&gt;"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">htmlspecialchars</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">defaultHandler</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">) == </font><font color="#DD0000">"&amp;" </font><font color="#007700">&amp;&amp; </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">) == </font><font color="#DD0000">";"</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">'&lt;font color="#aa00aa"&gt;%s&lt;/font&gt;'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">htmlspecialchars</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">'&lt;font size="-1"&gt;%s&lt;/font&gt;'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">htmlspecialchars</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">externalEntityRefHandler</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$openEntityNames</font><font color="#007700">, </font><font color="#0000BB">$base</font><font color="#007700">, </font><font color="#0000BB">$systemId</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$publicId</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$systemId</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!list(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$fp</font><font color="#007700">) = </font><font color="#0000BB">new_xml_parser</font><font color="#007700">(</font><font color="#0000BB">$systemId</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"Could not open entity %s at %s\n"</font><font color="#007700">, </font><font color="#0000BB">$openEntityNames</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$systemId</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">4096</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">xml_parse</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"XML error: %s at line %d while parsing entity %s\n"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_error_string</font><font color="#007700">(</font><font color="#0000BB">xml_get_error_code</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_get_current_line_number</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">), </font><font color="#0000BB">$openEntityNames</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$parser</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">new_xml_parser</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global </font><font color="#0000BB">$parser_file</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$xml_parser </font><font color="#007700">= </font><font color="#0000BB">xml_parser_create</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_parser_set_option</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">XML_OPTION_CASE_FOLDING</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_set_element_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"startElement"</font><font color="#007700">, </font><font color="#DD0000">"endElement"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_set_character_data_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"characterData"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_set_processing_instruction_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"PIHandler"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_set_default_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"defaultHandler"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_set_external_entity_ref_handler</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#DD0000">"externalEntityRefHandler"</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if (!(</font><font color="#0000BB">$fp </font><font color="#007700">= @</font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$parser_file</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">settype</font><font color="#007700">(</font><font color="#0000BB">$parser_file</font><font color="#007700">, </font><font color="#DD0000">"array"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$parser_file</font><font color="#007700">[</font><font color="#0000BB">$xml_parser</font><font color="#007700">] = </font><font color="#0000BB">$file</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return array(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">$fp</font><font color="#007700">);<br />}<br /><br />if (!(list(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">$fp</font><font color="#007700">) = </font><font color="#0000BB">new_xml_parser</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">"could not open XML input"</font><font color="#007700">);<br />}<br /><br />echo </font><font color="#DD0000">"&lt;pre&gt;"</font><font color="#007700">;<br />while (</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">4096</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">xml_parse</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">, </font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"XML error: %s at line %d\n"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_error_string</font><font color="#007700">(</font><font color="#0000BB">xml_get_error_code</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">xml_get_current_line_number</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">)));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br />echo </font><font color="#DD0000">"&lt;/pre&gt;"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"parse complete\n"</font><font color="#007700">;<br /></font><font color="#0000BB">xml_parser_free</font><font color="#007700">(</font><font color="#0000BB">$xml_parser</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN240704"
></A
><P
><B
>例 4. xmltest.xml</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="xml"
>&#60;?xml version='1.0'?&#62;
&#60;!DOCTYPE chapter SYSTEM "/just/a/test.dtd" [
&#60;!ENTITY plainEntity "FOO entity"&#62;
&#60;!ENTITY systemEntity SYSTEM "xmltest2.xml"&#62;
]&#62;
&#60;chapter&#62;
 &#60;TITLE&#62;Title &#38;plainEntity;&#60;/TITLE&#62;
 &#60;para&#62;
  &#60;informaltable&#62;
   &#60;tgroup cols="3"&#62;
    &#60;tbody&#62;
     &#60;row&#62;&#60;entry&#62;a1&#60;/entry&#62;&#60;entry morerows="1"&#62;b1&#60;/entry&#62;&#60;entry&#62;c1&#60;/entry&#62;&#60;/row&#62;
     &#60;row&#62;&#60;entry&#62;a2&#60;/entry&#62;&#60;entry&#62;c2&#60;/entry&#62;&#60;/row&#62;
     &#60;row&#62;&#60;entry&#62;a3&#60;/entry&#62;&#60;entry&#62;b3&#60;/entry&#62;&#60;entry&#62;c3&#60;/entry&#62;&#60;/row&#62;
    &#60;/tbody&#62;
   &#60;/tgroup&#62;
  &#60;/informaltable&#62;
 &#60;/para&#62;
 &#38;systemEntity;
 &#60;section id="about"&#62;
  &#60;title&#62;About this Document&#60;/title&#62;
  &#60;para&#62;
   &#60;!-- this is a comment --&#62;
   &#60;?php echo 'Hi!  This is PHP version ' . phpversion(); ?&#62;
  &#60;/para&#62;
 &#60;/section&#62;
&#60;/chapter&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     以下文档将被 <TT
CLASS="filename"
>xmltest.xml</TT
> 文件调用:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN240709"
></A
><P
><B
>例 5. xmltest2.xml</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="xml"
>&#60;?xml version="1.0"?&#62;
&#60;!DOCTYPE foo [
&#60;!ENTITY testEnt "test entity"&#62;
]&#62;
&#60;foo&#62;
   &#60;element attrib="value"/&#62;
   &#38;testEnt;
   &#60;?php echo "This is some more PHP code being executed."; ?&#62;
&#60;/foo&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>目录</B
></DT
><DT
><A
HREF="function.utf8-decode.html"
>utf8_decode</A
>&nbsp;--&nbsp;
	 将用 UTF-8 方式编码的 ISO-8859-1 字符串转换成单字节的 ISO-8859-1 字符串。
    </DT
><DT
><A
HREF="function.utf8-encode.html"
>utf8_encode</A
>&nbsp;--&nbsp;将 ISO-8859-1 编码的字符串转换为 UTF-8 编码</DT
><DT
><A
HREF="function.xml-error-string.html"
>xml_error_string</A
>&nbsp;--&nbsp;获取 XML 解析器的错误字符串</DT
><DT
><A
HREF="function.xml-get-current-byte-index.html"
>xml_get_current_byte_index</A
>&nbsp;--&nbsp;获取 XML 解析器的当前字节索引</DT
><DT
><A
HREF="function.xml-get-current-column-number.html"
>xml_get_current_column_number</A
>&nbsp;--&nbsp;
     获取 XML 解析器的当前列号
    </DT
><DT
><A
HREF="function.xml-get-current-line-number.html"
>xml_get_current_line_number</A
>&nbsp;--&nbsp;获取 XML 解析器的当前行号</DT
><DT
><A
HREF="function.xml-get-error-code.html"
>xml_get_error_code</A
>&nbsp;--&nbsp;获取 XML 解析器错误代码</DT
><DT
><A
HREF="function.xml-parse-into-struct.html"
>xml_parse_into_struct</A
>&nbsp;--&nbsp;将 XML 数据解析到数组中</DT
><DT
><A
HREF="function.xml-parse.html"
>xml_parse</A
>&nbsp;--&nbsp;开始解析一个 XML 文档</DT
><DT
><A
HREF="function.xml-parser-create-ns.html"
>xml_parser_create_ns</A
>&nbsp;--&nbsp;
     生成一个支持命名空间的 XML 解析器
    </DT
><DT
><A
HREF="function.xml-parser-create.html"
>xml_parser_create</A
>&nbsp;--&nbsp;建立一个 XML 解析器</DT
><DT
><A
HREF="function.xml-parser-free.html"
>xml_parser_free</A
>&nbsp;--&nbsp;释放指定的 XML 解析器</DT
><DT
><A
HREF="function.xml-parser-get-option.html"
>xml_parser_get_option</A
>&nbsp;--&nbsp;从 XML 解析器获取选项设置信息</DT
><DT
><A
HREF="function.xml-parser-set-option.html"
>xml_parser_set_option</A
>&nbsp;--&nbsp;为指定 XML 解析进行选项设置</DT
><DT
><A
HREF="function.xml-set-character-data-handler.html"
>xml_set_character_data_handler</A
>&nbsp;--&nbsp;建立字符数据处理器</DT
><DT
><A
HREF="function.xml-set-default-handler.html"
>xml_set_default_handler</A
>&nbsp;--&nbsp;建立默认处理器</DT
><DT
><A
HREF="function.xml-set-element-handler.html"
>xml_set_element_handler</A
>&nbsp;--&nbsp;建立起始和终止元素处理器</DT
><DT
><A
HREF="function.xml-set-end-namespace-decl-handler.html"
>xml_set_end_namespace_decl_handler</A
>&nbsp;--&nbsp;
     建立终止命名空间声明处理器
    </DT
><DT
><A
HREF="function.xml-set-external-entity-ref-handler.html"
>xml_set_external_entity_ref_handler</A
>&nbsp;--&nbsp;建立外部实体指向处理器</DT
><DT
><A
HREF="function.xml-set-notation-decl-handler.html"
>xml_set_notation_decl_handler</A
>&nbsp;--&nbsp;建立注释声明处理器</DT
><DT
><A
HREF="function.xml-set-object.html"
>xml_set_object</A
>&nbsp;--&nbsp;在对象中使用 XML 解析器</DT
><DT
><A
HREF="function.xml-set-processing-instruction-handler.html"
>xml_set_processing_instruction_handler</A
>&nbsp;--&nbsp;
     建立处理指令(PI)处理器
    </DT
><DT
><A
HREF="function.xml-set-start-namespace-decl-handler.html"
>xml_set_start_namespace_decl_handler</A
>&nbsp;--&nbsp;
     建立起始命名空间声明处理器
    </DT
><DT
><A
HREF="function.xml-set-unparsed-entity-decl-handler.html"
>xml_set_unparsed_entity_decl_handler</A
>&nbsp;--&nbsp;
     建立未解析实体定义声明处理器
    </DT
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.xdiff-string-patch.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.utf8-decode.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>xdiff_string_patch</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>utf8_decode</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>