设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
小说专版  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专   题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: IT公社 IT community >> 网络编程 >> PHP >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
PHP has encountered an…
PHP中for循环语句的几种…
为什么PHP令人不爽(对于…
如何利用PHP和CSS改变网…
PHP程序与服务器端通讯的…
PHPUnit袖珍指南之自动测…
用php或js获取图片大小,…
还是说php实现singleton…
PHP实现简单线性回归之数…
使用PHP的Socket写的POP…
  在PHP5中使用DOM控制XML(2)         
在PHP5中使用DOM控制XML(2)
 

//向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>
Linux联盟收集整理

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2007-1-3 3:33:34
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:itqoo    责任编辑:itqoo 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持