這次給大家帶來PHP動態建立XML文檔步驟詳解,PHP動態建立XML文檔的注意事項有哪些,下面就是實戰案例,一起來看一下。
一. 代碼
conn.php
<?php$id=mysql_connect("localhost","root","root") or die('資料庫連接失敗:' . mysql_error());if(mysql_select_db("db_database26",$id)) echo ""; else echo ('資料庫錯誤' . mysql_error());mysql_query("set names gb2312");?>
index.php
<aref="rss.xml">查看rss.xml檔案中的內容</a><?phpinclude_once("conn/conn.php");include_once("rss.php");?>
rss.php
<?php$self=$_SERVER['HTTP_REFERER'];$u=$_SERVER['HTTP_HOST'];$url="http://"."$u";$date_time=date("Y-m-d H:i:s");$dom = new DomDocument('1.0','gb2312'); //建立DOM對象$object = $dom->createElement('rss'); //建立根節點rss$dom->appendChild($object); //將建立的根節點添加到dom對象中 $type1 = $dom->createAttribute('xmlns:rdf'); //建立一個節點屬性xmlns:rdf $object->appendChild($type1); //將屬性追加到rss根節點中 $type1_value = $dom->createTextNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#'); //建立一個屬性值 $type1->appendChild($type1_value); //將屬性值賦給屬性xmlns:rdf $type2 = $dom->createAttribute('xmlns:dc'); //建立一個節點屬性xmlns:dc $object->appendChild($type2); //將屬性追加到rss根節點中 $type2_value = $dom->createTextNode('http://purl.org/dc/elements/1.1/'); //建立一個屬性值 $type2->appendChild($type2_value); //將屬性值賦給屬性xmlns:dc $type3 = $dom->createAttribute('xmlns:taxo'); //建立一個節點屬性xmlns:taxo $object->appendChild($type3); //將屬性追加到rss根節點中 $type3_value = $dom->createTextNode('http://purl.org/rss/1.0/modules/taxonomy/'); //建立一個屬性值 $type3->appendChild($type3_value); //將屬性值賦給屬性xmlns:taxo $type4 = $dom->createAttribute('version'); //建立一個節點屬性version $object->appendChild($type4); //將屬性追加到rss根節點中 $type4_value = $dom->createTextNode('2.0'); //建立一個屬性值 $type4->appendChild($type4_value); //將屬性值賦給屬性version $channel = $dom->createElement('channel'); //建立節點channel $object->appendChild($channel); //將節點channel追加到根節點rss下 $title = $dom->createElement('title'); //建立節點title $channel->appendChild($title); //將節點追加到channel節點下 $title_value = $dom->createTextNode(iconv('gb2312','utf-8','明日科技')); //建立元素值 $title->appendChild($title_value); //將值賦給title節點 $link = $dom->createElement('link'); //建立節點link $channel->appendChild($link); //將節點追加到channel節點下 $link_value = $dom->createTextNode(iconv('gb2312','utf-8','http://www.mingrisoft.com'));//建立元素值 $link->appendChild($link_value); //將值賦給link節點 $description = $dom->createElement('description'); //建立節點description $channel->appendChild($description); //將節點追加到channel節點下 $description_value = $dom->createTextNode(iconv('gb2312','utf-8','明日科技')); //建立元素值 $description->appendChild($description_value); //將值賦給description節點 $dc_creator = $dom->createElement('dc:creator'); //建立節點dc:creator $channel->appendChild($dc_creator); //將節點追加到channel節點中 $dc_creator_value = $dom->createTextNode(iconv('gb2312','utf-8','http://www.mingrisoft.com'));//建立元素值 $dc_creator->appendChild($dc_creator_value); //將值賦給dc:creator節點$sql=mysql_query("select * from tb_rss_database order by tb_rss_id desc"); //從資料庫中讀取資料while($myrow=mysql_fetch_array($sql)){ //迴圈輸出資料庫中資料 $item = $dom->createElement('item'); //建立節點item $object->appendChild($item); //將item追加到channel節點下 $item_title = $dom->createElement('title'); //建立title節點 $item->appendChild($item_title); //將節點追加到item節點下 $item_link = $dom->createElement('link'); //建立link節點 $item->appendChild($item_link); //將節點追加到item節點下 $item_description = $dom->createElement('description'); //建立description節點 $item->appendChild($item_description); //將節點追加到item節點中 $item_pubDate = $dom->createElement('pubDate'); //建立節點pubDate $item->appendChild($item_pubDate); //將節點追加到item節點下 $title_value = $dom->createTextNode(iconv('gb2312','utf-8',"$myrow[tb_rss_subject]")); //建立元素值 $item_title->appendChild($title_value); //將值賦給title節點 $link_value = $dom->createTextNode(iconv('gb2312','utf-8',"$url/tm/sl/22/02/look_content.php?lmbs=$myrow[tb_rss_id]"));//建立元素值 $item_link->appendChild($link_value); //將值賦給link節點 $description=substr($myrow[tb_rss_content],0,80); //截取該欄位中的前80個字元 $description_value = $dom->createTextNode(iconv('gb2312','utf-8',"$description"));//建立元素值 $item_description->appendChild($description_value); //將值賦給description節點 $pubDate_value = $dom->createTextNode(iconv('gb2312','utf-8',"$date_time"));//建立元素值 $item_pubDate->appendChild($pubDate_value); //將值賦給pubDate節點}$modi = $dom->saveXML(); //產生xml文檔file_put_contents('Rss.xml',$modi); /* 將對象儲存到Rss.xml文檔中 */?>
二. 運行結果
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
PHP裝飾器模式使用詳解
thinkPHP架構自動填滿原理與用法使用詳解