PHP實現(xiàn)動態(tài)創(chuàng)建XML文檔的方法
本文實例講述了PHP實現(xiàn)動態(tài)創(chuàng)建XML文檔的方法。分享給大家供大家參考,具體如下:
一. 代碼
conn.php
<?php $id=mysql_connect("localhost","root","root") or die('數(shù)據(jù)庫連接失敗:' . mysql_error()); if(mysql_select_db("db_database26",$id)) echo ""; else echo ('數(shù)據(jù)庫錯誤' . mysql_error()); mysql_query("set names gb2312"); ?>
index.php
<a ref="rss.xml">查看rss.xml文件中的內(nèi)容</a> <?php include_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'); //創(chuàng)建DOM對象 $object = $dom->createElement('rss'); //創(chuàng)建根節(jié)點rss $dom->appendChild($object); //將創(chuàng)建的根節(jié)點添加到dom對象中 $type1 = $dom->createAttribute('xmlns:rdf'); //創(chuàng)建一個節(jié)點屬性xmlns:rdf $object->appendChild($type1); //將屬性追加到rss根節(jié)點中 $type1_value = $dom->createTextNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#'); //創(chuàng)建一個屬性值 $type1->appendChild($type1_value); //將屬性值賦給屬性xmlns:rdf $type2 = $dom->createAttribute('xmlns:dc'); //創(chuàng)建一個節(jié)點屬性xmlns:dc $object->appendChild($type2); //將屬性追加到rss根節(jié)點中 $type2_value = $dom->createTextNode('http://purl.org/dc/elements/1.1/'); //創(chuàng)建一個屬性值 $type2->appendChild($type2_value); //將屬性值賦給屬性xmlns:dc $type3 = $dom->createAttribute('xmlns:taxo'); //創(chuàng)建一個節(jié)點屬性xmlns:taxo $object->appendChild($type3); //將屬性追加到rss根節(jié)點中 $type3_value = $dom->createTextNode('http://purl.org/rss/1.0/modules/taxonomy/'); //創(chuàng)建一個屬性值 $type3->appendChild($type3_value); //將屬性值賦給屬性xmlns:taxo $type4 = $dom->createAttribute('version'); //創(chuàng)建一個節(jié)點屬性version $object->appendChild($type4); //將屬性追加到rss根節(jié)點中 $type4_value = $dom->createTextNode('2.0'); //創(chuàng)建一個屬性值 $type4->appendChild($type4_value); //將屬性值賦給屬性version $channel = $dom->createElement('channel'); //創(chuàng)建節(jié)點channel $object->appendChild($channel); //將節(jié)點channel追加到根節(jié)點rss下 $title = $dom->createElement('title'); //創(chuàng)建節(jié)點title $channel->appendChild($title); //將節(jié)點追加到channel節(jié)點下 $title_value = $dom->createTextNode(iconv('gb2312','utf-8','明日科技')); //創(chuàng)建元素值 $title->appendChild($title_value); //將值賦給title節(jié)點 $link = $dom->createElement('link'); //創(chuàng)建節(jié)點link $channel->appendChild($link); //將節(jié)點追加到channel節(jié)點下 $link_value = $dom->createTextNode(iconv('gb2312','utf-8','http://www.mingrisoft.com'));//創(chuàng)建元素值 $link->appendChild($link_value); //將值賦給link節(jié)點 $description = $dom->createElement('description'); //創(chuàng)建節(jié)點description $channel->appendChild($description); //將節(jié)點追加到channel節(jié)點下 $description_value = $dom->createTextNode(iconv('gb2312','utf-8','明日科技')); //創(chuàng)建元素值 $description->appendChild($description_value); //將值賦給description節(jié)點 $dc_creator = $dom->createElement('dc:creator'); //創(chuàng)建節(jié)點dc:creator $channel->appendChild($dc_creator); //將節(jié)點追加到channel節(jié)點中 $dc_creator_value = $dom->createTextNode(iconv('gb2312','utf-8','http://www.mingrisoft.com'));//創(chuàng)建元素值 $dc_creator->appendChild($dc_creator_value); //將值賦給dc:creator節(jié)點 $sql=mysql_query("select * from tb_rss_database order by tb_rss_id desc"); //從數(shù)據(jù)庫中讀取數(shù)據(jù) while($myrow=mysql_fetch_array($sql)){ //循環(huán)輸出數(shù)據(jù)庫中數(shù)據(jù) $item = $dom->createElement('item'); //創(chuàng)建節(jié)點item $object->appendChild($item); //將item追加到channel節(jié)點下 $item_title = $dom->createElement('title'); //創(chuàng)建title節(jié)點 $item->appendChild($item_title); //將節(jié)點追加到item節(jié)點下 $item_link = $dom->createElement('link'); //創(chuàng)建link節(jié)點 $item->appendChild($item_link); //將節(jié)點追加到item節(jié)點下 $item_description = $dom->createElement('description'); //創(chuàng)建description節(jié)點 $item->appendChild($item_description); //將節(jié)點追加到item節(jié)點中 $item_pubDate = $dom->createElement('pubDate'); //創(chuàng)建節(jié)點pubDate $item->appendChild($item_pubDate); //將節(jié)點追加到item節(jié)點下 $title_value = $dom->createTextNode(iconv('gb2312','utf-8',"$myrow[tb_rss_subject]")); //創(chuàng)建元素值 $item_title->appendChild($title_value); //將值賦給title節(jié)點 $link_value = $dom->createTextNode(iconv('gb2312','utf-8',"$url/tm/sl/22/02/look_content.php?lmbs=$myrow[tb_rss_id]"));//創(chuàng)建元素值 $item_link->appendChild($link_value); //將值賦給link節(jié)點 $description=substr($myrow[tb_rss_content],0,80); //截取該字段中的前80個字符 $description_value = $dom->createTextNode(iconv('gb2312','utf-8',"$description"));//創(chuàng)建元素值 $item_description->appendChild($description_value); //將值賦給description節(jié)點 $pubDate_value = $dom->createTextNode(iconv('gb2312','utf-8',"$date_time"));//創(chuàng)建元素值 $item_pubDate->appendChild($pubDate_value); //將值賦給pubDate節(jié)點 } $modi = $dom->saveXML(); //生成xml文檔 file_put_contents('Rss.xml',$modi); /* 將對象保存到Rss.xml文檔中 */ ?>
二. 運行結果
PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP實現(xiàn)陽歷到農(nóng)歷轉(zhuǎn)換的類實例
這篇文章主要介紹了PHP實現(xiàn)陽歷到農(nóng)歷轉(zhuǎn)換的類,實例分析了陽歷轉(zhuǎn)換到陰歷的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03