<?php
$file = "xmltest.xml";
//验证文件的合法性
function trustedFile($file) {
// only trust local files owned by ourselves
if (!eregi("^([a-z]+)://", $file)
&& fileowner($file) == getmyuid()) {
return true;
}
return false;
}
//处理起始标记的函数。用特殊颜色标记并输出显示。
//注意$attribs为数组
function startElement($parser, $name, $attribs=array() ) {
print "<<font color="#0000cc">$name</font>";
if (sizeof($attribs)) {
while (list($k, $v) = each($attribs)) {
print " <font color="#009900">$k</font>="<font
color="#990000">$v</font>"";
}
}
print ">";
}
//结束标记处理并显示
function endElement($parser, $name) {
print "</<font color="#0000cc">$name</font>>";
}
//处理数据部分
function characterData($parser, $data) {
print "<b>$data</b>";
}
//处理指令(PI)处理器 参数处理函数
function PIHandler($parser, $target, $data) {
switch (strtolower($target)) {
case "php":
global $parser_file;
// If the parsed document is "trusted", we say it is safe
// to execute PHP code inside it. If not, display the code
// instead.
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 %sn", $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 %sn",
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;
}
//xml分析器。
function new_xml_parser($file) {
global $parser_file;
$xml_parser = xml_parser_create(); //建立一个 XML 解析器,此函数返回解释器的操作句柄。
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1); //设置是否采用大小写折叠,及目标编码
xml_set_element_handler($xml_parser, "startElement", "endElement");//建立起始和终止元素处理器,bool
xml_set_character_data_handler($xml_parser, "characterData");//建立字符数据处理器,bool
xml_set_processing_instruction_handler($xml_parser, "PIHandler");//建立处理指令(PI)处理器
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");//将文件处理变量设为array类型
}
$parser_file[$xml_parser] = $file; //?将文件名赋值给以解释器操作句柄为索引的数组?(解释器的句柄以资源记录的形式返回)
// echo "<font color=red >parser = ";
// print_r($parser_file);
// echo "<br>$xml_parser";
// echo "</font>";
return array($xml_parser, $fp); //解释器的操作句柄 和待分析文件的句柄
}
if (!(list($xml_parser, $fp) = new_xml_parser($file))) {
die("could not open XML input");
}
print "<pre>";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {//此处采用条件赋值。当条件表达式失效时执行if处理,否则跳过。
die(sprintf("XML error: %s at line %dn",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
print "</pre>";
print "parse completen";
xml_parser_free($xml_parser);
?>
xmltest.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--因为对xml了解不是很深,故将实体引用部分略去了 -->
<chapter>
<TITLE>Title </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>
<section id="about">
<title>About this Document</title>
<para>
<!-- this is a comment -->
<?php print 'Hi! This is PHP version '.phpversion(); ?>
</para>
</section>
</chapter>
还有一个是将xml文件处理成php数组的例子。
<?php
class AminoAcid {
var $name; // aa name
var $symbol; // three letter symbol
var $code; // one letter code
var $type; // hydrophobic, charged or neutral
function AminoAcid ($aa) {
foreach ($aa as $k=>$v)
$this->$k = $aa[$k];
}
}
function readDatabase($filename) {
// read the xml database of aminoacids
$data = implode("",file($filename));//首先将整篇文章读入数组,之后再将数组连接成字符串,赋值给$data.
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);//不使用大小写折叠
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($parser,$data,$values,$tags);//将 XML 数据解析到数组中,该函数将 XML 文件解析到两个对应的数组中,
//$tags 参数含有指向 $values 数组中对应值的指针。最后两个数组参数可由指针传递给函数。
xml_parser_free($parser);
// loop through the structures
//针对具体的应用(不同的xml文件,修改此处循环结构得到具体的数组即可。
foreach ($tags as $key=>$val) {
if ($key == "molecule") {
$molranges = $val;
// each contiguous pair of array entries are the
// lower and upper range for each molecule definition
for ($i=0; $i < count($molranges); $i+=2) {
$offset = $molranges[$i] + 1;
$len = $molranges[$i + 1] - $offset;
$tdb[] = parseMol(array_slice($values, $offset, $len));
}
} else {
continue;
}
}
// echo "<font color=red>values is:";
// print_r($values);
// echo "</font>";
return array($tdb,$values);
}
function parseMol($mvalues) {
for ($i=0; $i < count($mvalues); $i++)
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
// echo "<font color=blue> after parsemol :";
// print_r($mol);
// echo "</font>";
return new AminoAcid($mol);
}
$db = readDatabase("moldb.xml");
echo "** Database of AminoAcid objects:n";
// echo "<font color=purple> readdatabase :";
print_r($db[0]);
// echo "</font>";
$s = parseMol($db[1]);
?>
副 moldb.xml
<?xml version="1.0" encoding="UTF-8" ?>
<moldb>
<molecule>
<name>Alanine</name>
<symbol>ala</symbol>
<code>A</code>
<type>hydrophobic</type>
</molecule>
<molecule>
<name>Lysine</name>
<symbol>lys</symbol>
<code>K</code>
<type>charged</type>
</molecule>
</moldb>
|