用PHP解析XML 的方法 1 php5的dom解析2 php4的expat解析作3. SimpleXM 提供了一种高效的检索xml文档的方法4. 其他还有xslt、xpath等方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://cn2.php.net/manual/en/book.xml.php php解析xml有现成的函数,可以参考手册 一本书上的例子,可以参考parse.php<html> <head><title>My Library</title></head> <body><?phpclass BookList{ var $parser,$record,$current_field='',$field_type,$ends_record,$records; function BookList($filename){ $this->parser=xml_parser_create("utf-8"); xml_set_object($this->parser,&$this); xml_set_element_handler($this->parser,start_element,end_element); xml_set_character_data_handler($this->parser,cdata); //1=单个字段,2=数组字段,3=记录容器 $this->field_type=array('title'=>1, 'author'=>2, 'isbn'=>1, 'comment'=>1 ); $this->ends_record=array("book"=>true); $x=join("",file($filename)); xml_parse($this->parser,$x); xml_parser_free($this->parser); } function start_element($p,$element,&$attributes){ $element=strtolower($element); if($this->field_type[$element]!=0){ $this->current_field=$element; }else{ $this->current_field=''; } } function end_element($p,$element){ $element=strtolower($element); if($this->ends_record[$element]){ $this->records[]=$this->record; $this->record=array(); } $this->current_field=''; } function cdata($p,$text){ if($this->field_type[$this->current_field]===2){ $this->record[$this->current_field][]=$text; }elseif($this->field_type[$this->current_field]===1){ $this->record[$this->current_field]=$text; } } function show_menu() { echo "<table border='1'>\n"; foreach($this->records as $book){ echo "<tr>"; $authors=join(',',$book['author']); printf("<th><a href='%s'>%s</a></th><td>%s</td>\n", $_SERVER['PHP_SELF'].'?isbn='.$book['isbn'], $book['title'], $authors); echo "</tr>\n"; } echo "</table>"; } function show_book($isbn){ $Find=false; foreach($this->records as $book){ if($book['isbn']===$isbn){ $author=join(',',$book['author']); printf("<b>%s</b> by %s.<br>",$book['title'],$author); printf("ISBN:%s<br>",$isbn); printf("Comment:%s\n",$book['comment']); $Find=true; break; } } if(!$Find)echo "<b style='color:red'>Can't find this book of '$isbn'</b>"; echo '<p>Back to the <a href="'.$_SERVER['PHP_SELF'].'">list of books</a>.</p>'; }}$lib=new BookList("book.xml");if($_GET['isbn']){ $lib->show_book($_GET['isbn']);}else{ $lib->show_menu();}?></body></html>book.xml<?xml version="1.0" encoding="utf-8"?><library> <book> <title><![CDATA[PHP编程]]></title> <authors> <author>Rasmus Lerdorf</author> <author>Kevin Tatroe</author> </authors> <isbn>1-56592-610-2</isbn> <comment>A greate book!</comment> </book> <book> <title><![CDATA[PHP参考手册]]></title> <authors> <author>Rasmus Lerdorf</author> </authors> <isbn>1-56592-769-9</isbn> <comment>It really does fit in your pocket!</comment> </book> <book> <title><![CDATA[Perl手册]]></title> <authors> <author>Tom Christiansen</author> <author>Nathan Torkington</author> </authors> <isbn>1-56692-243-3</isbn> <comment>Hundreds of useful techniques,most just as applicable to PHP as tc perl</comment> </book></library> $xml ="a.xml"; $dom = new DOMDocument(); $dom->load($xml); $xml_value = $dom->getElementsByTagName('data');//标签 foreach($xml_value as $value){ $nodevalue = $value->nodeValue; } 什么也不用安装,推荐两种方式simplexml 比较简单,但是功能也少dom比较全面,和msxml类似,做过其他语言开发的人很容易上手这个 Zend_Db_Table与Zend_Db_Table_Abstract的区别? 请教一下php调用存储过程问题. 请问用php怎么表示这两个正则表达式? 数据存储和程序设计的疑问 php怎么实现这种功能.100分 fckeditor插入图片时弹出图像属性窗口的大小控制? 用户在线问题 php连接access数据库中连不到!求助!!! 问一个IF条件判断的问题. php怎样获取是什么app(微信,支付宝,云闪付)访问了链接 Php群 诚邀各位加入 为什么我的一张带有flash的php网页在本地测试时flash无法显示
parse.php
<html>
<head><title>My Library</title></head>
<body>
<?php
class BookList{
var $parser,$record,$current_field='',$field_type,$ends_record,$records;
function BookList($filename){
$this->parser=xml_parser_create("utf-8");
xml_set_object($this->parser,&$this);
xml_set_element_handler($this->parser,start_element,end_element);
xml_set_character_data_handler($this->parser,cdata);
//1=单个字段,2=数组字段,3=记录容器
$this->field_type=array('title'=>1,
'author'=>2,
'isbn'=>1,
'comment'=>1 );
$this->ends_record=array("book"=>true);
$x=join("",file($filename));
xml_parse($this->parser,$x);
xml_parser_free($this->parser);
}
function start_element($p,$element,&$attributes){
$element=strtolower($element);
if($this->field_type[$element]!=0){
$this->current_field=$element;
}else{
$this->current_field='';
}
}
function end_element($p,$element){
$element=strtolower($element);
if($this->ends_record[$element]){
$this->records[]=$this->record;
$this->record=array();
}
$this->current_field='';
}
function cdata($p,$text){
if($this->field_type[$this->current_field]===2){
$this->record[$this->current_field][]=$text;
}elseif($this->field_type[$this->current_field]===1){
$this->record[$this->current_field]=$text;
}
}
function show_menu()
{
echo "<table border='1'>\n";
foreach($this->records as $book){
echo "<tr>";
$authors=join(',',$book['author']);
printf("<th><a href='%s'>%s</a></th><td>%s</td>\n",
$_SERVER['PHP_SELF'].'?isbn='.$book['isbn'],
$book['title'],
$authors);
echo "</tr>\n";
}
echo "</table>";
}
function show_book($isbn){
$Find=false;
foreach($this->records as $book){
if($book['isbn']===$isbn){
$author=join(',',$book['author']);
printf("<b>%s</b> by %s.<br>",$book['title'],$author);
printf("ISBN:%s<br>",$isbn);
printf("Comment:%s\n",$book['comment']);
$Find=true;
break;
}
}
if(!$Find)echo "<b style='color:red'>Can't find this book of '$isbn'</b>";
echo '<p>Back to the <a href="'.$_SERVER['PHP_SELF'].'">list of books</a>.</p>';
}
}$lib=new BookList("book.xml");
if($_GET['isbn']){
$lib->show_book($_GET['isbn']);
}
else{
$lib->show_menu();
}
?>
</body>
</html>book.xml
<?xml version="1.0" encoding="utf-8"?>
<library>
<book>
<title><![CDATA[PHP编程]]></title>
<authors>
<author>Rasmus Lerdorf</author>
<author>Kevin Tatroe</author>
</authors>
<isbn>1-56592-610-2</isbn>
<comment>A greate book!</comment>
</book>
<book>
<title><![CDATA[PHP参考手册]]></title>
<authors>
<author>Rasmus Lerdorf</author>
</authors>
<isbn>1-56592-769-9</isbn>
<comment>It really does fit in your pocket!</comment>
</book>
<book>
<title><![CDATA[Perl手册]]></title>
<authors>
<author>Tom Christiansen</author>
<author>Nathan Torkington</author>
</authors>
<isbn>1-56692-243-3</isbn>
<comment>Hundreds of useful techniques,most just as applicable to PHP as tc perl</comment>
</book>
</library>
$xml ="a.xml";
$dom = new DOMDocument();
$dom->load($xml);
$xml_value = $dom->getElementsByTagName('data');//标签
foreach($xml_value as $value){
$nodevalue = $value->nodeValue;
}
推荐两种方式
simplexml 比较简单,但是功能也少
dom比较全面,和msxml类似,做过其他语言开发的人很容易上手这个