≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

PHP5使用DOM控制XML功能实例

人气:392 时间:2020-11-27

这篇文章主要为大家详细介绍了PHP5使用DOM控制XML功能实例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!

下面的例子简单的演示了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> 

本文来自:http://www.q1010.com/173/13640-0.html

注:关于PHP5使用DOM控制XML功能实例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:

您可能感兴趣的文章

上一篇:php数据库导出成excel的简单示例
下一篇:PHP 文件上传的简单实现方法
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。