DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 用PHP實現動態產生XML文件以及從XML文件中抽取數據轉化成HTM
用PHP實現動態產生XML文件以及從XML文件中抽取數據轉化成HTM
編輯:XML詳解     
我一天。。。可是到最後發現卻無法實現自己想像中的那種效果。。。真是可惜。。。看來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\"?>\n";
$XMLfile.="<books>\n";

while($row=MySQL_fetch_array($rs)){
    for($i=0;$i<$numfIElds;$i++){
        $fIEldname=MySQL_fIEld_name($rs,$i);
        $XMLfile.="<" . $fieldname . ">" . $row[$i] . "</" . $fIEldname . ">\n";
    }
}
MySQL_free_result($rs);
MySQL_close($connect_id);
$XMLfile.="</books>\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 "<tr><td colspan=\"2\" align=\"center\">" . $tag . "</td>";
        }
        else{
            switch ((XML_get_current_line_number($parser)-2)%6){
                case 0:
                    echo "<tr><td>下載<td>";
                    break;
                case 1:
                    echo "<tr><td>ID號<td>";
                    break;
                case 2:
                    echo "<tr><td>書名<td>";
                    break;
                case 3:
                    echo "<tr><td>作者<td>";
                    break;
                case 4:
                    echo "<tr><td>出版社<td>";
                    break;
                case 5:
                    echo "<tr><td>出版日期<td>";
                    break;
            }
        }
    }
    
    function cdata($parser,$cdata){
        echo $cdata;
    }
    
    function tag_off($parser,$tag){
        echo "\n";
    }
}

$xml_parser = new XML();
$xmlfilename="xmldoc/xmldoc.XML";
$fp=fopen($XMLfilename,"r");
$xmldata=fread($fp,filesize($XMLfilename));
?>
<Html><head><title>圖書信息</title></head>
<body>
<table border="0" cellspacing="1" cellpadding="2" width="80%" bgcolor="#b0d8fF" align="center">
<?
$xml_parser->parse($XMLdata);
?>
</table>

其實你們應該看得出。。這個例子是個失敗的東東。。。因為效果不大。。根本不能得到自己想要的東東。。。唉。。。苦。。。。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved