设为首页 收藏本站
查看: 805|回复: 0

[经验分享] 在PHP5中使用DOM控制XML

[复制链接]

尚未签到

发表于 2015-11-17 14:51:52 | 显示全部楼层 |阅读模式
PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为 PHP5 核心的一部分,无需被安装即可使用。
下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释
<?
/************************************************
**                    use XML in PHP5
** reference site:
** http://cn.php.net/manual/zh/ref.dom.php
** the follow codes need PHP5 support
** www.knowsky.com
*************************************************/


//首先要创建一个DOMDocument对象
$dom = new DomDocument();
//然后载入XML文件
$dom -> load(&quot;test.xml&quot;);

//输出XML文件
//header(&quot;Content-type: text/xml;charset=gb2312&quot;);
//echo $dom -> saveXML();

//保存XML文件,返回&#20540;为int(文件大小,以字节为单位)
//$dom -> save(&quot;newfile.xml&quot;);

echo &quot;<hr/>取得所有的title元素:<hr/>&quot;;
$titles = $dom -> getElementsByTagName(&quot;title&quot;);
foreach ($titles as $node){
  echo $node -> textContent . &quot;<br/>&quot;;
  //这样也可以
  //echo $node->firstChild->data . &quot;<br/>&quot;;
}

/*
echo &quot;<hr/>从根结点遍历所有结点:<br/>&quot;;
foreach ($dom->documentElement->childNodes as $items) {
  //如果节点是一个元素(nodeType == 1)并且名字是item就继续循环
  if ($items->nodeType == 1 && $items->nodeName == &quot;item&quot;) {
    foreach ($items->childNodes as $titles) {
      //如果节点是一个元素,并且名字是title就打印它.
      if ($titles->nodeType == 1 && $titles->nodeName == &quot;title&quot;) {
        print $titles->textContent . &quot;\n&quot;;
      }
    }
  }
}
*/

//使用XPath查询数据
echo &quot;<hr/>使用XPath查询的title节点结果:<hr/>&quot;;
$xpath = new domxpath($dom);
$titles = $xpath->query(&quot;/rss/channel/item/title&quot;);
foreach ($titles as $node){
  echo $node->textContent.&quot;<br/>&quot;;
}
/*
这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多
深入一点可能是这样:
/rss/channel/item[position() = 1]/title 返回第一个item元素的所有
/rss/channel/item/title[@id = '23'] 返回所有含有id属性并且&#20540;为23的title
/rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度)
*/


//向DOM中写入新数据
$item = $dom->createElement(&quot;item&quot;);
$title = $dom->createElement(&quot;title&quot;);
$titleText = $dom->createTextNode(&quot;title text&quot;);
$title->appendChild($titleText);
$item->appendChild($title);
$dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item);

//从DOM中删除节点
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName(&quot;channel&quot;)->item(0));
//或者使用xpath查询出节点再删除
//$dom->documentElement->RemoveChild($xpath->query(&quot;/rss/channel&quot;)->item(0));
//$dom->save(&quot;newfile.xml&quot;);

//从DOM中修改节点数据
//修改第一个title的文件
//这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我
$firstTitle = $xpath->query(&quot;/rss/channel/item/title&quot;)->item(0);
$newTitle = $dom->createElement(&quot;title&quot;);
$newTitle->appendChild(new DOMText(&quot;This's the new title text!!!&quot;));
$firstTitle->parentNode->replaceChild($newTitle, $firstTitle);
//修改属性
//$firstTitle = $xpath->query(&quot;/rss/channel/item/title&quot;)->item(0);
//$firstTitle->setAttribute(&quot;orderby&quot;, &quot;4&quot;);
$dom->save(&quot;newfile.xml&quot;);

echo &quot;<hr/><a href=\&quot;newfile.xml\&quot;>查看newfile.xml</a>&quot;;
//下面的代码获得并解析php.net的首页,将返第一个title元素的内容。
/*
$dom->loadHTMLFile(&quot;http://www.php.net/&quot;);
$title = $dom->getElementsByTagName(&quot;title&quot;);
print $title->item(0)->textContent;
*/
?>

下面是test.xml文件代码:
<?xml version=&quot;1.0&quot; encoding=&quot;gb2312&quot;?>
<rss version=&quot;2.0&quot;>
<channel>
<title>javascript</title>
<link>http://blog.iyunv.com/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=&quot;1&quot;>out put excel used javascript</title>
<link>http://blog.iyunv.com/zhongmao/archive/2004/09/15/105385.aspx</link>
<pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate>
<guid>http://blog.iyunv.com/zhongmao/archive/2004/09/15/105385.aspx</guid>
<comment>http://blog.iyunv.com/zhongmao/comments/105385.aspx</comment>
<comments>http://blog.iyunv.com/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments>
<comments>2</comments>
<commentrss>http://blog.iyunv.com/zhongmao/comments/commentrss/105385.aspx</commentrss>
<ping>http://blog.iyunv.com/zhongmao/services/trackbacks/105385.aspx</ping>
<description>test description</description>
</item>
<item>
<creator>zhongmao</creator>
<title orderby=&quot;2&quot;>out put word used javascript</title>
<link>http://blog.iyunv.com/zhongmao/archive/2004/08/06/67161.aspx</link>
<pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate>
<guid>http://blog.iyunv.com/zhongmao/archive/2004/08/06/67161.aspx</guid>
<comment>http://blog.iyunv.com/zhongmao/comments/67161.aspx</comment>
<comments>http://blog.iyunv.com/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments>
<comments>0</comments>
<commentrss>http://blog.iyunv.com/zhongmao/comments/commentrss/67161.aspx</commentrss>
<ping>http://blog.iyunv.com/zhongmao/services/trackbacks/67161.aspx</ping>
<description>test word description</description>
</item>
<item>
<creator>zhongmao</creator>
<title orderby=&quot;3&quot;>xmlhttp</title>
<link>http://blog.iyunv.com/zhongmao/archive/2004/08/02/58417.aspx</link>
<pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate>
<guid>http://blog.iyunv.com/zhongmao/archive/2004/08/02/58417.aspx</guid>
<comment>http://blog.iyunv.com/zhongmao/comments/58417.aspx</comment>
<comments>http://blog.iyunv.com/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments>
<comments>0</comments>
<commentrss>http://blog.iyunv.com/zhongmao/comments/commentrss/58417.aspx</commentrss>
<ping>http://blog.iyunv.com/zhongmao/services/trackbacks/58417.aspx</ping>
<description>xmlhttpaaa asd bb cc dd</description>
</item>
</channel>
</rss>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-140402-1-1.html 上篇帖子: 在WIN32下搭建PHP开发环境,部署HDWiki 下篇帖子: Visual Studio ---PHP开发的插件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表