本文實(shí)例講述了PHP實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建XML文檔的方法。分享給大家供大家參考,具體如下:
一. 代碼
conn.php
<?php $id=mysql_connect("localhost","root","root") or die('數(shù)據(jù)庫(kù)連接失敗:' . mysql_error()); if(mysql_select_db("db_database26",$id)) echo ""; else echo ('數(shù)據(jù)庫(kù)錯(cuò)誤' . 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對(duì)象 $object = $dom->createElement('rss'); //創(chuàng)建根節(jié)點(diǎn)rss $dom->appendChild($object); //將創(chuàng)建的根節(jié)點(diǎn)添加到dom對(duì)象中 $type1 = $dom->createAttribute('xmlns:rdf'); //創(chuàng)建一個(gè)節(jié)點(diǎn)屬性xmlns:rdf $object->appendChild($type1); //將屬性追加到rss根節(jié)點(diǎn)中 $type1_value = $dom->createTextNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#'); //創(chuàng)建一個(gè)屬性值 $type1->appendChild($type1_value); //將屬性值賦給屬性xmlns:rdf $type2 = $dom->createAttribute('xmlns:dc'); //創(chuàng)建一個(gè)節(jié)點(diǎn)屬性xmlns:dc $object->appendChild($type2); //將屬性追加到rss根節(jié)點(diǎn)中 $type2_value = $dom->createTextNode('http://purl.org/dc/elements/1.1/'); //創(chuàng)建一個(gè)屬性值 $type2->appendChild($type2_value); //將屬性值賦給屬性xmlns:dc $type3 = $dom->createAttribute('xmlns:taxo'); //創(chuàng)建一個(gè)節(jié)點(diǎn)屬性xmlns:taxo $object->appendChild($type3); //將屬性追加到rss根節(jié)點(diǎn)中 $type3_value = $dom->createTextNode('http://purl.org/rss/1.0/modules/taxonomy/'); //創(chuàng)建一個(gè)屬性值 $type3->appendChild($type3_value); //將屬性值賦給屬性xmlns:taxo $type4 = $dom->createAttribute('version'); //創(chuàng)建一個(gè)節(jié)點(diǎn)屬性version $object->appendChild($type4); //將屬性追加到rss根節(jié)點(diǎn)中 $type4_value = $dom->createTextNode('2.0'); //創(chuàng)建一個(gè)屬性值 $type4->appendChild($type4_value); //將屬性值賦給屬性version $channel = $dom->createElement('channel'); //創(chuàng)建節(jié)點(diǎn)channel $object->appendChild($channel); //將節(jié)點(diǎn)channel追加到根節(jié)點(diǎn)rss下 $title = $dom->createElement('title'); //創(chuàng)建節(jié)點(diǎn)title $channel->appendChild($title); //將節(jié)點(diǎn)追加到channel節(jié)點(diǎn)下 $title_value = $dom->createTextNode(iconv('gb2312','utf-8','明日科技')); //創(chuàng)建元素值 $title->appendChild($title_value); //將值賦給title節(jié)點(diǎn) $link = $dom->createElement('link'); //創(chuàng)建節(jié)點(diǎn)link $channel->appendChild($link); //將節(jié)點(diǎn)追加到channel節(jié)點(diǎn)下 $link_value = $dom->createTextNode(iconv('gb2312','utf-8','http://www.mingrisoft.com'));//創(chuàng)建元素值 $link->appendChild($link_value); //將值賦給link節(jié)點(diǎn) $description = $dom->createElement('description'); //創(chuàng)建節(jié)點(diǎn)description $channel->appendChild($description); //將節(jié)點(diǎn)追加到channel節(jié)點(diǎn)下 $description_value = $dom->createTextNode(iconv('gb2312','utf-8','明日科技')); //創(chuàng)建元素值 $description->appendChild($description_value); //將值賦給description節(jié)點(diǎn) $dc_creator = $dom->createElement('dc:creator'); //創(chuàng)建節(jié)點(diǎn)dc:creator $channel->appendChild($dc_creator); //將節(jié)點(diǎn)追加到channel節(jié)點(diǎn)中 $dc_creator_value = $dom->createTextNode(iconv('gb2312','utf-8','http://www.mingrisoft.com'));//創(chuàng)建元素值 $dc_creator->appendChild($dc_creator_value); //將值賦給dc:creator節(jié)點(diǎn) $sql=mysql_query("select * from tb_rss_database order by tb_rss_id desc"); //從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù) while($myrow=mysql_fetch_array($sql)){ //循環(huán)輸出數(shù)據(jù)庫(kù)中數(shù)據(jù) $item = $dom->createElement('item'); //創(chuàng)建節(jié)點(diǎn)item $object->appendChild($item); //將item追加到channel節(jié)點(diǎn)下 $item_title = $dom->createElement('title'); //創(chuàng)建title節(jié)點(diǎn) $item->appendChild($item_title); //將節(jié)點(diǎn)追加到item節(jié)點(diǎn)下 $item_link = $dom->createElement('link'); //創(chuàng)建link節(jié)點(diǎn) $item->appendChild($item_link); //將節(jié)點(diǎn)追加到item節(jié)點(diǎn)下 $item_description = $dom->createElement('description'); //創(chuàng)建description節(jié)點(diǎn) $item->appendChild($item_description); //將節(jié)點(diǎn)追加到item節(jié)點(diǎn)中 $item_pubDate = $dom->createElement('pubDate'); //創(chuàng)建節(jié)點(diǎn)pubDate $item->appendChild($item_pubDate); //將節(jié)點(diǎn)追加到item節(jié)點(diǎn)下 $title_value = $dom->createTextNode(iconv('gb2312','utf-8',"$myrow[tb_rss_subject]")); //創(chuàng)建元素值 $item_title->appendChild($title_value); //將值賦給title節(jié)點(diǎn) $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é)點(diǎn) $description=substr($myrow[tb_rss_content],0,80); //截取該字段中的前80個(gè)字符 $description_value = $dom->createTextNode(iconv('gb2312','utf-8',"$description"));//創(chuàng)建元素值 $item_description->appendChild($description_value); //將值賦給description節(jié)點(diǎn) $pubDate_value = $dom->createTextNode(iconv('gb2312','utf-8',"$date_time"));//創(chuàng)建元素值 $item_pubDate->appendChild($pubDate_value); //將值賦給pubDate節(jié)點(diǎn) } $modi = $dom->saveXML(); //生成xml文檔 file_put_contents('Rss.xml',$modi); /* 將對(duì)象保存到Rss.xml文檔中 */ ?>
二. 運(yùn)行結(jié)果
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。