文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>在PHP5中使用DOM控制XML(2)

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

时间:2008-05-19  来源:剑心通明


//向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 "查看newfile.xml";
   
  //下面的代码获得并解析php.net的首页,将返第一个title元素的内容。
  /*
  $dom->loadHTMLFile("http://www.php.net/");
  $title = $dom->getElementsByTagName("title");
  print $title->item(0)->textContent;
  */
  ?>
   
  下面是test.xml文件代码:
   
   
   
   
  javascript
  http://blog.csdn.net/zhongmao/category/29515.aspx
  javascript
  zh-chs
  .text version 0.958.2004.2001
   
  zhongmao
  out put excel used javascript
  http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx
  wed, 15 sep 2004 13:32:00 gmt
  http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx
  http://blog.csdn.net/zhongmao/comments/105385.aspx
  http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback
  2
  http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx
  http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx
  test description
   
   
  zhongmao
  out put word used javascript
  http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx
  fri, 06 aug 2004 16:33:00 gmt
  http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx
  http://blog.csdn.net/zhongmao/comments/67161.aspx
  http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback
  0
  http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx
  http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx
  test word description
   
   
  zhongmao
  xmlhttp
  http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx
  mon, 02 aug 2004 10:11:00 gmt
  http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx
  http://blog.csdn.net/zhongmao/comments/58417.aspx
  http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback
  0
  http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx
  http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx
  xmlhttpaaa asd bb cc dd
   
   
   


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载