RSS订阅
PHP程序员站--WWW.PHPERZ.COM  
网站地图
高级搜索
收藏本站

 当前位置:主页 >> PHP基础 >> 基础文章 >> 文章内容
在PHP5中使用DOM控制XML(2)
[收藏此页[打印本页]   
来源:  作者:phpchina  发布时间:2007-12-26

 //向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的文件
  //这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我 www.phperz.com
  $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");

www.phperz.com


  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> www~phperz~com
  <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> php程序员站
  <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> phperz.com
  <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程序员之家


 
 相关文章
 
发表评论
全部评论(0条)
 
 站内搜索
 热门搜索 基础  mysql  url  adodb
高级搜索 网站地图 站长工具 IP查询 收藏本站
 热点文章
 随机推荐
网站首页 | 网站地图 | 高级搜索 | RSS订阅
PHP程序员站 Copyright © 2007,PHPERZ.COM All Rights Reserved 粤ICP备07503606号 联系站长