PHP5中使用DOM控制XML实现代码


Posted in PHP onMay 07, 2010

下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释

<? 
/************************************************ 
** use XML in PHP5 
** reference site: 
** http://cn.php.net/manual/zh/ref.dom.php 
** the follow codes need PHP5 support 
*************************************************/ //首先要创建一个DOMDocument对象 
$dom = new DomDocument(); 
//然后载入XML文件 
$dom -> load("test.xml"); 
//输出XML文件 
//header("Content-type: text/xml;charset=gb2312"); 
//echo $dom -> saveXML(); 
//保存XML文件,返回值为int(文件大小,以字节为单位) 
//$dom -> save("newfile.xml"); 
echo "<hr/>取得所有的title元素:<hr/>"; 
$titles = $dom -> getElementsByTagName("title"); 
foreach ($titles as $node){ 
echo $node -> textContent . "<br/>"; 
//这样也可以 
//echo $node->firstChild->data . "<br/>"; 
} 
/* 
echo "<hr/>从根结点遍历所有结点:<br/>"; 
foreach ($dom->documentElement->childNodes as $items) { 
//如果节点是一个元素(nodeType == 1)并且名字是item就继续循环 
if ($items->nodeType == 1 && $items->nodeName == "item") { 
foreach ($items->childNodes as $titles) { 
//如果节点是一个元素,并且名字是title就打印它. 
if ($titles->nodeType == 1 && $titles->nodeName == "title") { 
print $titles->textContent . "\n"; 
} 
} 
} 
} 
*/ 
//使用XPath查询数据 
echo "<hr/>使用XPath查询的title节点结果:<hr/>"; 
$xpath = new domxpath($dom); 
$titles = $xpath->query("/rss/channel/item/title"); 
foreach ($titles as $node){ 
echo $node->textContent."<br/>"; 
} 
/* 
这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多 
深入一点可能是这样: 
/rss/channel/item[position() = 1]/title 返回第一个item元素的所有 
/rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title 
/rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度) 
*/ 

//向DOM中写入新数据 
$item = $dom->createElement("item"); 
$title = $dom->createElement("title"); 
$titleText = $dom->createTextNode("title text"); 
$title->appendChild($titleText); 
$item->appendChild($title); 
$dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item); 
//从DOM中删除节点 
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0)); 
//或者使用xpath查询出节点再删除 
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0)); 
//$dom->save("newfile.xml"); 
//从DOM中修改节点数据 
//修改第一个title的文件 
//这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我 
$firstTitle = $xpath->query("/rss/channel/item/title")->item(0); 
$newTitle = $dom->createElement("title"); 
$newTitle->appendChild(new DOMText("This's the new title text!!!")); 
$firstTitle->parentNode->replaceChild($newTitle, $firstTitle); 
//修改属性 
//$firstTitle = $xpath->query("/rss/channel/item/title")->item(0); 
//$firstTitle->setAttribute("orderby", "4"); 
$dom->save("newfile.xml"); 
echo "<hr/><a href=\"newfile.xml\">查看newfile.xml</a>"; 
//下面的代码获得并解析php.net的首页,将返第一个title元素的内容。 
/* 
$dom->loadHTMLFile("http://www.php.net/"); 
$title = $dom->getElementsByTagName("title"); 
print $title->item(0)->textContent; 
*/ 
?>

下面是test.xml文件代码:
<?xml version="1.0" encoding="gb2312"?> 
<rss version="2.0"> 
<channel> 
<title>javascript</title> 
<link>http://blog.csdn.net/zhongmao/category/29515.aspx</link> 
<description>javascript</description> 
<language>zh-chs</language> 
<generator>.text version 0.958.2004.2001</generator> 
<item> 
<creator>zhongmao</creator> 
<title orderby="1">out put excel used javascript</title> 
<link>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</link> 
<pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate> 
<guid>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</guid> 
<comment>http://blog.csdn.net/zhongmao/comments/105385.aspx</comment> 
<comments>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments> 
<comments>2</comments> 
<commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx</commentrss> 
<ping>http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx</ping> 
<description>test description</description> 
</item> 
<item> 
<creator>zhongmao</creator> 
<title orderby="2">out put word used javascript</title> 
<link>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</link> 
<pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate> 
<guid>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</guid> 
<comment>http://blog.csdn.net/zhongmao/comments/67161.aspx</comment> 
<comments>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments> 
<comments>0</comments> 
<commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx</commentrss> 
<ping>http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx</ping> 
<description>test word description</description> 
</item> 
<item> 
<creator>zhongmao</creator> 
<title orderby="3">xmlhttp</title> 
<link>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</link> 
<pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate> 
<guid>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</guid> 
<comment>http://blog.csdn.net/zhongmao/comments/58417.aspx</comment> 
<comments>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments> 
<comments>0</comments> 
<commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx</commentrss> 
<ping>http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx</ping> 
<description>xmlhttpaaa asd bb cc dd</description> 
</item> 
</channel> 
</rss>
PHP 相关文章推荐
GD输出汉字的函数的分析
Oct 09 PHP
php中json_encode中文编码问题分析
Sep 13 PHP
php MessagePack介绍
Oct 06 PHP
php使用websocket示例详解
Mar 12 PHP
单台服务器的PHP进程之间实现共享内存的方法
Jun 13 PHP
Thinkphp实现MySQL读写分离操作示例
Jun 25 PHP
用php来限制每个ip每天浏览页面数量的实现思路
Feb 24 PHP
PHP MVC框架路由学习笔记
Mar 02 PHP
PHP十六进制颜色随机生成器功能示例
Jul 24 PHP
解决windows上php xdebug 无法调试的问题
Feb 19 PHP
PHP安装扩展mcrypt以及相关依赖项深入讲解
Mar 04 PHP
PHP引擎php.ini参数优化深入讲解
Mar 24 PHP
PHP 金额数字转换成英文
May 06 #PHP
php源码加密 仿微盾PHP加密专家(PHPCodeLock)
May 06 #PHP
基于asp+ajax和数据库驱动的二级联动菜单
May 06 #PHP
PHP 类商品秒杀计时实现代码
May 05 #PHP
PHP 面向对象 final类与final方法
May 05 #PHP
PHP 面向对象 PHP5 中的常量
May 05 #PHP
在Windows下编译适用于PHP 5.2.12及5.2.13的eAccelerator.dll(附下载)
May 04 #PHP
You might like
php strtotime 函数UNIX时间戳
2009/01/14 PHP
深入php多态的实现详解
2013/06/09 PHP
ucenter通信原理分析
2015/01/09 PHP
laravel 修改记住我功能的cookie保存时间的方法
2019/10/14 PHP
php 中的信号处理操作实例详解
2020/03/04 PHP
js下判断 iframe 是否加载完成的完美方法
2010/10/26 Javascript
Js 弹出框口并返回值的两种常用方法
2010/12/30 Javascript
jQuery操作checkbox选择(list/table)
2013/04/07 Javascript
Ext JS添加子组件的误区探讨
2013/06/28 Javascript
JavaScript实现简单的数字倒计时
2015/05/15 Javascript
JQuery操作textarea,input,select,checkbox方法
2015/09/02 Javascript
全面解析Bootstrap弹窗的实现方法
2015/12/01 Javascript
详解axios 全攻略之基本介绍与使用(GET 与 POST)
2017/09/15 Javascript
vue实现点击关注后及时更新列表功能
2018/06/26 Javascript
JS使用JSON.parse(),JSON.stringify()实现对对象的深拷贝功能分析
2019/03/06 Javascript
Vue-drag-resize 拖拽缩放插件的使用(简单示例)
2019/12/04 Javascript
微信小程序实现简单文字跑马灯
2020/05/26 Javascript
jQuery实现开关灯效果
2020/08/02 jQuery
[02:55]2018DOTA2国际邀请赛勇士令状不朽珍藏Ⅲ饰品一览
2018/08/01 DOTA
Python爬虫_城市公交、地铁站点和线路数据采集实例
2018/01/10 Python
python3实现名片管理系统
2020/11/29 Python
手把手教你使用Python创建微信机器人
2019/04/29 Python
tensorflow将图片保存为tfrecord和tfrecord的读取方式
2020/02/17 Python
Django models文件模型变更错误解决
2020/05/11 Python
PIL.Image.open和cv2.imread的比较与相互转换的方法
2020/06/03 Python
Canvas 文本转粒子效果的实现代码
2019/02/14 HTML / CSS
HTML5移动端开发中的Viewport标签及相关CSS用法解析
2016/04/15 HTML / CSS
美国购买舞会礼服网站:Couture Candy
2019/12/29 全球购物
实习生个人的自我评价
2013/12/08 职场文书
2014年元旦促销活动方案
2014/02/22 职场文书
综合内勤岗位职责
2014/04/14 职场文书
2014年节能减排工作总结
2014/12/06 职场文书
2015年七一建党节活动总结
2015/03/20 职场文书
承诺书应该怎么写?
2019/09/10 职场文书
MySQL如何解决幻读问题
2021/08/07 MySQL
Python中np.random.randint()参数详解及用法实例
2022/09/23 Python