Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17757866
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-04-17 17:34:41

以下范例将 文档中的标记符直接映射成 标记符。在“映射数组”中不存在的元素将被忽略。当然,该范例将只对一个特定的 XML 文档有效。

$file = "data.xml";
$map_array = array(
   "BOLD"    => "B",
   "EMPHASIS" => "I",
   "LITERAL"  => "TT"
);

function startElement($parser, $name, $attrs) {
   global $map_array;
   if ($htmltag == $map_array[$name]) {
       print "<$htmltag>";
   }
}

function endElement($parser, $name) {
   global $map_array;
   if ($htmltag == $map_array[$name]) {
       print "";
   }
}

function characterData($parser, $data) {
   print $data;
}

$xml_parser = xml_parser_create();
// 使用大小写折叠来保证我们能在元素数组中找到这些元素名称
xml_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);
?> 

阅读(355) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~