在PHP5中使用DOM控制XML

来源:互联网 发布:mac 显示文件路径命令 编辑:程序博客网 时间:2024/05/13 16:25
<script type="text/javascript"><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

 http://www.corange.cn/archives/2008/10/2005.html

  1. PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为 PHP5 核心的一部分,无需被安装即可使用。 
  2. 下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释 
  3. <? 
  4. /************************************************ 
  5. ** use XML in PHP5 
  6. ** reference site: 
  7. ** http://cn.php.net/manual/zh/ref.dom.php 
  8. ** the follow codes need PHP5 support 
  9. ** www.knowsky.com 
  10. *************************************************/ 
  11. //首先要创建一个DOMDocument对象 
  12. $dom = new DomDocument(); 
  13. //然后载入XML文件 
  14. $dom -> load("test.xml"); 
  15. //输出XML文件 
  16. //header("Content-type: text/xml;charset=gb2312"); 
  17. //echo $dom -> saveXML(); 
  18. //保存XML文件,返回值为int(文件大小,以字节为单位) 
  19. //$dom -> save("newfile.xml"); 
  20. echo "<hr/>取得所有的title元素:<hr/>"
  21. $titles = $dom -> getElementsByTagName("title"); 
  22. foreach ($titles as $node){ 
  23. echo $node -> textContent . "<br/>"
  24. //这样也可以 
  25. //echo $node->firstChild->data . "<br/>"; 
  26. /* 
  27. echo "<hr/>从根结点遍历所有结点:<br/>"; 
  28. foreach ($dom->documentElement->childNodes as $items) { 
  29. //如果节点是一个元素(nodeType == 1)并且名字是item就继续循环 
  30. if ($items->nodeType == 1 && $items->nodeName == "item") { 
  31. foreach ($items->childNodes as $titles) { 
  32. //如果节点是一个元素,并且名字是title就打印它. 
  33. if ($titles->nodeType == 1 && $titles->nodeName == "title") { 
  34. print $titles->textContent . "/n"; 
  35. */ 
  36. //使用XPath查询数据 
  37. echo "<hr/>使用XPath查询的title节点结果:<hr/>"
  38. $xpath = new domxpath($dom); 
  39. $titles = $xpath->query("/rss/channel/item/title"); 
  40. foreach ($titles as $node){ 
  41. echo $node->textContent."<br/>"
  42. /* 
  43. 这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多 
  44. 深入一点可能是这样: 
  45. /rss/channel/item[position() = 1]/title 返回第一个item元素的所有 
  46. /rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title 
  47. /rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度) 
  48. */ 
  49. //向DOM中写入新数据 
  50. $item = $dom->createElement("item"); 
  51. $title = $dom->createElement("title"); 
  52. $titleText = $dom->createTextNode("title text"); 
  53. $title->appendChild($titleText); 
  54. $item->appendChild($title); 
  55. $dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item); 
  56. //从DOM中删除节点 
  57. //$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0)); 
  58. //或者使用xpath查询出节点再删除 
  59. //$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0)); 
  60. //$dom->save("newfile.xml"); 
  61. //从DOM中修改节点数据 
  62. //修改第一个title的文件 
  63. //这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我 
  64. $firstTitle = $xpath->query("/rss/channel/item/title")->item(0); 
  65. $newTitle = $dom->createElement("title"); 
  66. $newTitle->appendChild(new DOMText("This's the new title text!!!")); 
  67. $firstTitle->parentNode->replaceChild($newTitle$firstTitle); 
  68. //修改属性 
  69. //$firstTitle = $xpath->query("/rss/channel/item/title")->item(0); 
  70. //$firstTitle->setAttribute("orderby", "4"); 
  71. $dom->save("newfile.xml"); 
  72. echo "<hr/><a href=/"newfile.xml/">查看newfile.xml</a>"
  73. //下面的代码获得并解析php.net的首页,将返第一个title元素的内容。 
  74. /* 
  75. $dom->loadHTMLFile("http://www.php.net/"); 
  76. $title = $dom->getElementsByTagName("title"); 
  77. print $title->item(0)->textContent; 
  78. */ 
  79. ?> 
  80. 下面是test.xml文件代码: 
  81. <?xml version="1.0" encoding="gb2312"?> 
  82. <rss version="2.0"
  83. <channel> 
  84. <title>javascript</title> 
  85. <link>http://blog.csdn.net/zhongmao/category/29515.aspx</link> 
  86. <description>javascript</description> 
  87. <language>zh-chs</language> 
  88. <generator>.text version 0.958.2004.2001</generator> 
  89. <item> 
  90. <creator>zhongmao</creator> 
  91. <title orderby="1">out put excel used javascript</title> 
  92. <link>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</link> 
  93. <pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate> 
  94. <guid>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</guid> 
  95. <comment>http://blog.csdn.net/zhongmao/comments/105385.aspx</comment> 
  96. <comments>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments> 
  97. <comments>2</comments> 
  98. <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspx</commentrss> 
  99. <ping>http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx</ping> 
  100. <description>test description</description> 
  101. </item> 
  102. <item> 
  103. <creator>zhongmao</creator> 
  104. <title orderby="2">out put word used javascript</title> 
  105. <link>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</link> 
  106. <pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate> 
  107. <guid>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</guid> 
  108. <comment>http://blog.csdn.net/zhongmao/comments/67161.aspx</comment> 
  109. <comments>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments> 
  110. <comments>0</comments> 
  111. <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspx</commentrss> 
  112. <ping>http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx</ping> 
  113. <description>test word description</description> 
  114. </item> 
  115. <item> 
  116. <creator>zhongmao</creator> 
  117. <title orderby="3">xmlhttp</title> 
  118. <link>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</link> 
  119. <pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate> 
  120. <guid>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</guid> 
  121. <comment>http://blog.csdn.net/zhongmao/comments/58417.aspx</comment> 
  122. <comments>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments> 
  123. <comments>0</comments> 
  124. <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/58417.aspx</commentrss> 
  125. <ping>http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx</ping> 
  126. <description>xmlhttpaaa asd bb cc dd</description> 
  127. </item> 
  128. </channel> 
  129. </rss>