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语言流程控制中的主动与被动
Nov 05 PHP
有关phpmailer的详细介绍及使用方法
Jan 28 PHP
使用php实现下载生成某链接快捷方式的解决方法
May 07 PHP
深入理解:XML与对象的序列化与反序列化
Jun 08 PHP
解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
Jun 24 PHP
PHP判断是否为空的几个函数对比
Apr 21 PHP
百度地图API使用方法详解
Aug 25 PHP
CodeIgniter 完美解决URL含有中文字符串
May 13 PHP
PHP实现导出excel数据的类库用法示例
Oct 15 PHP
PHP 实现文件压缩解压操作的方法
Jun 14 PHP
PHP实现简单登录界面
Oct 23 PHP
php下的原生ajax请求用法实例分析
Feb 28 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函数解决SQL injection
2006/12/09 PHP
基于PHP读取csv文件内容的详解
2013/06/18 PHP
php实现mysql数据库分表分段备份
2015/06/18 PHP
php解决和避免form表单重复提交的几种方法
2016/08/31 PHP
php对接java现实加签验签的实例
2016/11/25 PHP
javascript知识点收藏
2007/02/22 Javascript
JS对select控件option选项的增删改查示例代码
2013/10/21 Javascript
JS 打印界面的CSS居中代码适用所有浏览器
2014/03/19 Javascript
node.js中的querystring.escape方法使用说明
2014/12/10 Javascript
jQuery实现DIV层收缩展开的方法
2015/02/27 Javascript
JavaScript的MVVM库Vue.js入门学习笔记
2016/05/03 Javascript
Bootstrap插件全集
2016/07/18 Javascript
JQuery 设置checkbox值二次无效的解决方法
2016/07/22 Javascript
Bootstrap实现的标签页内容切换显示效果示例
2017/05/25 Javascript
JS实现不用中间变量temp 实现两个变量值得交换方法
2018/02/04 Javascript
webpack 代码分离优化快速指北
2019/05/18 Javascript
编写更好的JavaScript条件式和匹配条件的技巧(小结)
2019/06/27 Javascript
20个必会的JavaScript面试题(小结)
2019/07/02 Javascript
vue created钩子函数与mounted钩子函数的用法区别
2020/11/05 Javascript
python解析xml模块封装代码
2014/02/07 Python
python海龟绘图实例教程
2014/07/24 Python
python中os操作文件及文件路径实例汇总
2015/01/15 Python
Python计算回文数的方法
2015/03/11 Python
python 排序算法总结及实例详解
2016/09/28 Python
python 按不同维度求和,最值,均值的实例
2018/06/28 Python
对python使用telnet实现弱密码登录的方法详解
2019/01/26 Python
django商品分类及商品数据建模实例详解
2020/01/03 Python
Pycharm安装python库的方法
2020/11/24 Python
Python安装Bs4的多种方法
2020/11/28 Python
美国眼镜网站:EyeBuyDirect
2017/04/13 全球购物
巴西独家产品和现场演示购物网站:Shoptime
2019/07/11 全球购物
解释一下ruby中的特殊方法与特殊类
2013/02/26 面试题
个人思想理论学习的自我鉴定
2013/11/30 职场文书
2014年底个人工作总结
2015/03/10 职场文书
pytorch 中nn.Dropout的使用说明
2021/05/20 Python
MySQL多表查询机制
2022/03/17 MySQL