這個東東搞了我一天。。。可是到最後發現卻無法實現自己想像中的那種效果。。。真是可惜。。。看來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="<?xml version="1.0" encoding="GB2312"?>
";
$xmlfile.="<books>
";
while($row=mysql_fetch_array($rs)){
for($i=0;$i<$numfields;$i++){
$fieldname=mysql_field_name($rs,$i);
$xmlfile.="<" . $fieldname . ">" . $row[$i] . "</" . $fieldname . ">
";
}
}
mysql_free_result($rs);
mysql_close($connect_id);
$xmlfile.="</books>
";
$fp=fopen("xmldoc/xmldoc.xml","w");
if(fwrite($fp,$xmlfile)){
echo "寫入文件成功!";
}
else{
echo "寫入文件失敗!";
}
?>
這段是從XML裡取數據轉化成HTML的源代碼。。。
<?
class xml{
var $parser;