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 相关文章推荐
用PHP产生动态的影像图
Oct 09 PHP
PHP可变函数的使用详解
Jun 14 PHP
如何让thinkphp在模型中自动完成session赋值小教程
Sep 05 PHP
php用户注册信息验证正则表达式
Nov 12 PHP
基于laravel制作APP接口(API)
Mar 15 PHP
PHP 验证身份证是否合法的函数
Feb 09 PHP
基于PHP常用文件函数和目录函数整理
Aug 17 PHP
PHP开发中解决并发问题的几种实现方法分析
Nov 13 PHP
Laravel 微信小程序后端实现用户登录的示例代码
Nov 26 PHP
PHP实现笛卡尔积算法的实例讲解
Dec 22 PHP
Yii redis集合的基本使用教程
Jun 14 PHP
php去除数组中为0的元素的实例分析
Nov 17 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中一个完整表单处理实现代码
2011/11/10 PHP
ThinkPHP结合AjaxFileUploader实现无刷新文件上传的方法
2014/10/29 PHP
php将access数据库转换到mysql数据库的方法
2014/12/24 PHP
PHP 中魔术常量的实例详解
2017/10/26 PHP
使用XHProf查找PHP性能瓶颈的实例
2017/12/13 PHP
tp5(thinkPHP5框架)使用DB实现批量删除功能示例
2019/05/28 PHP
thinkphp5 模型实例化获得数据对象的教程
2019/10/18 PHP
javascript 事件绑定问题
2011/01/01 Javascript
理解Angular数据双向绑定
2016/01/10 Javascript
在javascript中创建对象的各种模式解析
2016/05/16 Javascript
浅析BootStrap Treeview的简单使用
2016/10/12 Javascript
angular ng-repeat数组中的数组实例
2017/02/18 Javascript
基于打包工具Webpack进行项目开发实例
2018/05/29 Javascript
在AngularJs中设置请求头信息(headers)的方法及不同方法的比较
2018/09/04 Javascript
Node.js+Express+Mysql 实现增删改查
2019/04/03 Javascript
浅谈Webpack4 Tree Shaking 终极优化指南
2019/11/18 Javascript
详解使用mocha对webpack打包的项目进行&quot;冒烟测试&quot;的大致流程
2020/04/27 Javascript
[46:44]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第二场 3月7日
2021/03/11 DOTA
使用python编写批量卸载手机中安装的android应用脚本
2014/07/21 Python
在Python中使用itertools模块中的组合函数的教程
2015/04/13 Python
python实现爬虫下载美女图片
2015/07/14 Python
Python实现信用卡系统(支持购物、转账、存取钱)
2016/06/24 Python
python实现各进制转换的总结大全
2017/06/18 Python
python实现分页效果
2017/10/25 Python
Python数据结构与算法之图的基本实现及迭代器实例详解
2017/12/12 Python
一百行python代码将图片转成字符画
2021/02/19 Python
pycharm 取消默认的右击运行unittest的方法
2018/11/29 Python
一个入门级python爬虫教程详解
2021/01/27 Python
pytorch 计算Parameter和FLOP的操作
2021/03/04 Python
用HTML5 Canvas API中的clearRect()方法实现橡皮擦功能
2016/03/15 HTML / CSS
HTML5 本地存储之如果没有数据库究竟会怎样
2013/04/25 HTML / CSS
欧洲最大的化妆品连锁公司:Douglas道格拉斯
2017/05/06 全球购物
波比布朗英国官网:Bobbi Brown英国
2017/11/13 全球购物
企业办公室主任岗位职责
2014/02/19 职场文书
网络管理员岗位职责
2014/03/17 职场文书
2015年十月一日放假通知
2015/08/18 职场文书