这个东东搞了我一天。。。可是到最后发现却无法实现自己想像中的那种效果。。。真是可惜。。。看来php对xml还需要加强。。。如果哪位对此有研究。欢迎来信探讨。。。
首先建表:
CREATE TABLE books (
bookid int(4) NOT NULL auto_increment,
bookname varchar(100) NOT NULL,
bookauth varchar(50) NOT NULL,
bookpublisher varchar(50) NOT NULL,
bookpubdate datetime NOT NULL,
bookurl varchar(50) NOT NULL,
KEY bookid (bookid),
);
然后。。。这段是从MySQL里取数据转化成XML的源代码:
$connect_id=mysql_connect("localhost","root","");
mysql_select_db("bbs",$connect_id);
$query="select * from books order by bookid";
$rs=mysql_query($query,$connect_id);
$numfields=mysql_num_fields($rs);
$XMLfile="n";
$XMLfile.="n";
while($row=mysql_fetch_array($rs)){
for($i=0;$i$fieldname=mysql_field_name($rs,$i);
$XMLfile.="" . $row[$i] . "" . $fieldname . ">n";
}
}
mysql_free_result($rs);
mysql_close($connect_id);
$XMLfile.="n";
$fp=fopen("XMLdoc/XMLdoc.XML","w");
if(fwrite($fp,$XMLfile)){
echo "写入文件成功!";
}
else{
echo "写入文件失败!";
}
?>
这段是从XML里取数据转化成HTML的源代码。。。
class XML{
var $parser;
function XML(){
$this->parser = XML_parser_create();
XML_set_object($this->parser,&$this);
XML_set_element_handler($this->parser,"tag_on","tag_off");
XML_set_character_data_handler($this->parser,"cdata");
}
function parse($data){
XML_parse($this->parser,$data);
}
function tag_on($parser,$tag,$attributes){
if(XML_get_current_line_number($parser)==2){
echo "
" . $tag . "";
}
else{
switch ((XML_get_current_line_number($parser)-2)%6){
case 0:
echo "
下载";
break;
case 1:
echo "
ID号";
break;
case 2:
echo "
书名";
break;
case 3:
echo "
作者";
break;
case 4:
echo "
出版社";
break;
case 5:
echo "