使用代码,文件名:test_xml_parser.php
<HTML>
<HEAD>
<TITLE>Test for Manuel Lemos’s XML parser PHP class</TITLE>
</HEAD>
<BODY>
<H1><CENTER>Test for Manuel Lemos’s XML parser PHP class</CENTER></H1>
<HR>
<?
require("xml_parser.php");Function DumpArray(&$array,$indent)
{
for(Reset($array),$node=0;$node<count($array);Next($array),$node++)
{
echo $indent."\"".Key($array)."\"=";
$value=$array[Key($array)];
if(GetType($value)=="array")
{
echo "\n".$indent."[\n";
DumpArray($value,$indent."\t");
echo $indent."]\n";
}
else
echo "\"$value\"\n";
}
}Function DumpStructure(&$structure,&$positions,$path)
{
echo "[".$positions[$path]["Line"].",".$positions[$path]["Column"].",".$positions[$path]["Byte"]."]";
if(GetType($structure[$path])=="array")
{
echo "<".$structure[$path]["Tag"].">";
for($element=0;$element<$structure[$path]["Elements"];$element++)
DumpStructure($structure,$positions,$path.",$element");
echo "</".$structure[$path]["Tag"].">";
}
else
echo $structure[$path];
}$file_name="example.xml";
$error=XMLParseFile($parser,$file_name,1,$file_name.".cache");
if(strcmp($error,""))
echo "<H2><CENTER>Parser error: $error</CENTER></H2>\n";
else
{
echo "<H2><CENTER>Parsed file structure</CENTER></H2>\n";
echo "<P>This example dumps the structure of the elements of an XML file by displaying the tags and data preceded by their positions in the file: line number, column number and file byte index.</P>\n";
echo "<PRE>";
DumpStructure($parser->structure,$parser->positions,"0");
echo "</PRE>\n";
}
?></BODY>
</HTML>
+++++++++++++++++++++++++++++++++++++++++++++++++
测试结果如下图:

+++++++++++++++++++++++++++++++++++++++++++++++++
下载完整的php通用xml解析类:php-xmlparser

3 Comments
沙发~
不错不错..
看不懂而已..
因为PHP和我不熟~
我个人比较倾向正则表达式解析。
Post a Comment