本文实例讲述了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
<a
ref="rss.xml">查看rss.xml文件中的内容</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'); //创建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文档中 */
?>
二. 运行结果
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
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相关内容感兴趣的读者可查看本站专题:《php针对xml文件操作技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!