用PHP&XML编制迷你搜索引擎(二)
时间:2007-02-17 来源:PHP爱好者
三、一个最简单的用PHP显示XML的范例
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
下面的程序是将解析XML并按照树形结构输出至浏览器,并显示每层的元素总数。
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
__________________________________________________________
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
<?
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$file = "demo.xml";// XML文件
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 解析XML文件的函数
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function xml_parse_from_file($parser, $file)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!file_exists($file))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
die("Can’t find file "$file".");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!($fp = @fopen($file, "r")))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
die("Can’t open file "$file".");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
while($data = fread($fp, 4096))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!xml_parse($parser, $data, feof($fp)))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
return(false);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
fclose($fp);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
return(true);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function start_element($parser, $name, $attrs)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
//遇到了开元素标记如<a href="link">就执行这一段,
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
//$name=a,$attrs为一个属性数组
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level,$levelcount,$maxlevel;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$level += 1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if($level>$maxlevel)$maxlevel=$level;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$levelcount[$level]+=1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
for($i=1;$i<=($level-1);$i++)echo"| ";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if($level>0)echo"+----";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "<font color=red>".trim($name)." </font>";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
while ( list( $key, $val ) = each( $attrs ) ) { //显示属性
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "<font color=green>$key => $val</font>; ";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function stop_element($parser, $name)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
//遇到了开元素标记如</body>就执行这一段,
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$level -= 1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function data($parser, $data)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(trim($data)!=""){
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo trim($data);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function showcount() //显示每一层的元素总数
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $maxlevel,$levelcount;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
for ($i=0;$i<=$maxlevel;$i++){
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo $levelcount[$i]." ";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level,$levelcount,$maxlevel;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$level = -1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 产生解析器的实例
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$parser = xml_parser_create();
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 设置处理函数
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_set_element_handler($parser, "start_element", "stop_element");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_set_character_data_handler($parser, "data");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 解析文件
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$ret = xml_parse_from_file($parser, $file);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!$ret)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
die(sprintf("XML error: %s at line %d",
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_error_string(xml_get_error_code($parser)),
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_get_current_line_number($parser)));
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 释放解析器
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_parser_free($parser);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
showcount();
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
?>
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
下面的程序是将解析XML并按照树形结构输出至浏览器,并显示每层的元素总数。
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
__________________________________________________________
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
<?
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$file = "demo.xml";// XML文件
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 解析XML文件的函数
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function xml_parse_from_file($parser, $file)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!file_exists($file))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
die("Can’t find file "$file".");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!($fp = @fopen($file, "r")))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
die("Can’t open file "$file".");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
while($data = fread($fp, 4096))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!xml_parse($parser, $data, feof($fp)))
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
return(false);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
fclose($fp);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
return(true);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function start_element($parser, $name, $attrs)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
//遇到了开元素标记如<a href="link">就执行这一段,
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
//$name=a,$attrs为一个属性数组
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level,$levelcount,$maxlevel;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$level += 1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if($level>$maxlevel)$maxlevel=$level;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$levelcount[$level]+=1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
for($i=1;$i<=($level-1);$i++)echo"| ";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if($level>0)echo"+----";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "<font color=red>".trim($name)." </font>";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
while ( list( $key, $val ) = each( $attrs ) ) { //显示属性
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "<font color=green>$key => $val</font>; ";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function stop_element($parser, $name)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
//遇到了开元素标记如</body>就执行这一段,
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$level -= 1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function data($parser, $data)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(trim($data)!=""){
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo trim($data);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
function showcount() //显示每一层的元素总数
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $maxlevel,$levelcount;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo "
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
for ($i=0;$i<=$maxlevel;$i++){
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
echo $levelcount[$i]." ";
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
global $level,$levelcount,$maxlevel;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$level = -1;
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 产生解析器的实例
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$parser = xml_parser_create();
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 设置处理函数
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_set_element_handler($parser, "start_element", "stop_element");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_set_character_data_handler($parser, "data");
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 解析文件
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
$ret = xml_parse_from_file($parser, $file);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
if(!$ret)
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
{
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
die(sprintf("XML error: %s at line %d",
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_error_string(xml_get_error_code($parser)),
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_get_current_line_number($parser)));
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
}
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
// 释放解析器
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
xml_parser_free($parser);
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
showcount();
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
www.chinaitpower.comA7OouqmKWY71Q9czancaDuY9b
?>
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
相关阅读 更多 +