当前位置: 首页 > 图文教程 > 网络编程 > PHP > 在PHP5中使用DOM控制XML(2)

PHP
MYSQL版本大于4.1问题 - PHPchina
怎么让用户点击一个连接后,把一个图片另存了 - PHPchina
武汉10月15日Phper聚会召集!!! - PHPchina
php如果不等待exec执行的程序创建的子进程? - PHPchina
哪位知道DISCUZ处理防SQL注入的代码是哪部分 - PHPchina
求教!我实在不知道哪里问题,在线等ing - PHPchina
怎样结束用户某一进程 - PHPchina
比对用户名密码能不能这样写? - PHPchina
求助:如何在PHP+mysql中实现数据备份? - PHPchina
大家看看这个配置对吗 - PHPchina
如何禁止require当前文件 - PHPchina
无法将回调函数放在类中? - PHPchina
村里 PHP代码高亮是怎么实现的? - PHPchina
apache安装后.服务里没有apache2这个服务! - PHPchina
请教一个小问题 - PHPchina
config.php里面是不是应该把多数参数设置为常量而不是变量? - PHPchina
请教高手一个问题 - PHPchina
如何让百度收录我的网站 ?? - PHPchina
谁能给个注入的简单语句? - PHPchina
求PHP站内搜索思路 - PHPchina

在PHP5中使用DOM控制XML(2)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 52 ::
收藏到网摘: n/a

//向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>