一、XML 元素结构例程

第一个例程缩进显示文档中的开始元素结构。


Example #1 显示 XML 元素结构


<?php$file = "data.xml";$depth = array();function startElement($parser, $name, $attrs){global $depth;for ($i = 0; $i < $depth[$parser]; $i++) {echo "  ";}echo "$name\n";$depth[$parser]++;}function endElement($parser, $name){global $depth;$depth[$parser]--;}$xml_parser = xml_parser_create();xml_set_element_handler($xml_parser, "startElement", "endElement");if (!($fp = fopen($file, "r"))) {die("could not open XML input");}while ($data = fread($fp, 4096)) {if (!xml_parse($xml_parser, $data, feof($fp))) {die(sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));}}xml_parser_free($xml_parser);?>


二、XML 标签映射例程

Example #1 将 XML 映射为 HTML


此例程直接地将 XML 标签映射为 HTML 标签。 在“map_array”中未找到的元素将被忽略。 当然,此例程只针对特定的 XML 文档类型起作用。


<?php$file = "data.xml";$map_array = array("BOLD"     => "B","EMPHASIS" => "I","LITERAL"  => "TT");function startElement($parser, $name, $attrs){global $map_array;if (isset($map_array[$name])) {echo "<$map_array[$name]>";}}function endElement($parser, $name){global $map_array;if (isset($map_array[$name])) {echo "</$map_array[$name]>";}}function characterData($parser, $data){echo $data;}$xml_parser = xml_parser_create();// use case-folding so we are sure to find the tag in $map_arrayxml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);xml_set_element_handler($xml_parser, "startElement", "endElement");xml_set_character_data_handler($xml_parser, "characterData");if (!($fp = fopen($file, "r"))) {die("could not open XML input");}while ($data = fread($fp, 4096)) {if (!xml_parse($xml_parser, $data, feof($fp))) {die(sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));}}xml_parser_free($xml_parser);?>


三、XML 外部实体例程

此例程用于加亮 XML 代码。举例说明如何使用外部实体引用来包含和解析其他文档, 及处理指令是如何被处理的,及判断处理指令所包含代码是否“可信任”的一种方法

用于此例程的 XML 文档位于此例程的下方( 和 )。


Example #1 外部实体例程


<?php$file = "xmltest.xml";function trustedFile($file){// 仅信任本地文件if (!preg_match("@^([a-z]+)\:\/\/@i", $file)&& fileowner($file) == getmyuid()) {return true;}return false;}function startElement($parser, $name, $attribs){echo "&lt;<font color=\"#0000cc\">$name</font>";if (count($attribs)) {foreach ($attribs as $k => $v) {echo " <font color=\"#009900\">$k</font>=\"<fontcolor=\"#990000\">$v</font>\"";}}echo "&gt;";}function endElement($parser, $name){echo "&lt;/<font color=\"#0000cc\">$name</font>&gt;";}function characterData($parser, $data){echo "<b>$data</b>";}function PIHandler($parser, $target, $data){switch (strtolower($target)) {case "php":global $parser_file;// 如何要解析的文档是“可信任”的, 则说明可安全// 地执行其内部的 PHP 代码。否则,显示代码内容。if (trustedFile($parser_file[$parser])) {eval($data);} else {printf("Untrusted PHP code: <i>%s</i>",htmlspecialchars($data));}break;}}function defaultHandler($parser, $data){if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {printf('<font color="#aa00aa">%s</font>',htmlspecialchars($data));} else {printf('<font size="-1">%s</font>',htmlspecialchars($data));}}function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,$publicId) {if ($systemId) {if (!list($parser, $fp) = new_xml_parser($systemId)) {printf("Could not open entity %s at %s\n", $openEntityNames,$systemId);return false;}while ($data = fread($fp, 4096)) {if (!xml_parse($parser, $data, feof($fp))) {printf("XML error: %s at line %d while parsing entity %s\n",xml_error_string(xml_get_error_code($parser)),xml_get_current_line_number($parser), $openEntityNames);xml_parser_free($parser);return false;}}xml_parser_free($parser);return true;}return false;}function new_xml_parser($file){global $parser_file;$xml_parser = xml_parser_create();xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);xml_set_element_handler($xml_parser, "startElement", "endElement");xml_set_character_data_handler($xml_parser, "characterData");xml_set_processing_instruction_handler($xml_parser, "PIHandler");xml_set_default_handler($xml_parser, "defaultHandler");xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");if (!($fp = @fopen($file, "r"))) {return false;}if (!is_array($parser_file)) {settype($parser_file, "array");}$parser_file[$xml_parser] = $file;return array($xml_parser, $fp);}if (!(list($xml_parser, $fp) = new_xml_parser($file))) {die("could not open XML input");}echo "<pre>";while ($data = fread($fp, 4096)) {if (!xml_parse($xml_parser, $data, feof($fp))) {die(sprintf("XML error: %s at line %d\n",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));}}echo "</pre>";echo "parse complete\n";xml_parser_free($xml_parser);?>

Example #2 xmltest.xml

<?xml version='1.0'?><!DOCTYPE chapter SYSTEM "/just/a/test.dtd" [<!ENTITY plainEntity "FOO entity"><!ENTITY systemEntity SYSTEM "xmltest2.xml">]><chapter> <TITLE>Title &plainEntity;</TITLE> <para>  <informaltable>   <tgroup cols="3">    <tbody>     <row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>     <row><entry>a2</entry><entry>c2</entry></row>     <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>    </tbody>   </tgroup>  </informaltable> </para> &systemEntity; <section id="about">  <title>About this Document</title>  <para>   <!-- this is a comment -->   <?php echo 'Hi!  This is PHP version ' . phpversion(); ?>  </para> </section></chapter>

此文件包含在 中:


Example #3 xmltest2.xml


<?xml version="1.0"?><!DOCTYPE foo [<!ENTITY testEnt "test entity">]><foo>   <element attrib="value"/>   &testEnt;   <?php echo "This is some more PHP code being executed."; ?></foo>

四、XML解析器函数

  • utf8_decode — 将用 UTF-8 方式编码的 ISO-8859-1 字符串转换成单字节的 ISO-8859-1 字符串。

  • utf8_encode — 将 ISO-8859-1 编码的字符串转换为 UTF-8 编码

  • xml_error_string — 获取 XML 解析器的错误字符串

  • xml_get_current_byte_index — 获取 XML 解析器的当前字节索引

  • xml_get_current_column_number — 获取 XML 解析器的当前列号

  • xml_get_current_line_number — 获取 XML 解析器的当前行号

  • xml_get_error_code — 获取 XML 解析器错误代码

  • xml_parse_into_struct — 将 XML 数据解析到数组中

  • xml_parse — 开始解析一个 XML 文档

  • xml_parser_create_ns — 生成一个支持命名空间的 XML 解析器

  • xml_parser_create — 建立一个 XML 解析器

  • xml_parser_free — 释放指定的 XML 解析器

  • xml_parser_get_option — 从 XML 解析器获取选项设置信息

  • xml_parser_set_option — 为指定 XML 解析进行选项设置

  • xml_set_character_data_handler — 建立字符数据处理器

  • xml_set_default_handler — 建立默认处理器

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

  • xml_set_end_namespace_decl_handler — 建立终止命名空间声明处理器

  • xml_set_external_entity_ref_handler — 建立外部实体指向处理器

  • xml_set_notation_decl_handler — 建立注释声明处理器

  • xml_set_object — 在对象中使用 XML 解析器

  • xml_set_processing_instruction_handler — 建立处理指令(PI)处理器

  • xml_set_start_namespace_decl_handler — 建立起始命名空间声明处理器

  • xml_set_unparsed_entity_decl_handler — 建立未解析实体定义声明处理器

以上就是以上就是的内容,更多相关内容请关注PHP中文网(www.php.cn)!的内容,更多相关内容请关注PHP中文网(www.php.cn)!

更多相关文章

  1. ARM协处理器各寄存器功能介绍
  2. 类似于非Sandy Bridge / xeon处理器的RAPL
  3. Java注解处理器(编译时注解)

随机推荐

  1. dev c++是什么
  2. c语言中预处理命令都必须以什么号开始
  3. printf用法详解
  4. c语言运算符的优先级
  5. vs2017怎么编写c语言
  6. c语言struct用法详解
  7. 如何用c语言实现单词统计
  8. &&在c语言中是什么意思?
  9. c语言程序总是从第一个定义的函数开始执
  10. c语言怎么判断奇偶数