本文實(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é)果
PS:這里再為大家提供幾款關(guān)于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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- 使用PHPWord生成word文檔的方法詳解
- PHP讀取word文檔的方法分析【基于COM組件】
- php刪除txt文件指定行及按行讀取txt文檔數(shù)據(jù)的方法
- PHPExcel導(dǎo)出2003和2007的excel文檔功能示例
- PHP生成word文檔的三種實(shí)現(xiàn)方式
- PHP庫(kù) 查詢Mongodb中的文檔ID的方法
- PHP實(shí)現(xiàn)仿百度文庫(kù),豆丁在線文檔效果(word,excel,ppt轉(zhuǎn)flash)
- PHP sdk文檔處理常用代碼示例解析