http://www.corange.cn/archives/2008/10/2005.html
- PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为PHP5核心的一部分,无需被安装即可使用。
- 下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释
- <?
- /************************************************
- **useXMLinPHP5
- **referencesite:
- **http://cn.php.net/manual/zh/ref.dom.php
- **thefollowcodesneedPHP5support
- **www.knowsky.com
- *************************************************/
- //首先要创建一个DOMDocument对象
- $dom=newDomDocument();
- //然后载入XML文件
- $dom->load("test.xml");
- //输出XML文件
- //header("Content-type:text/xml;charset=gb2312");
- //echo$dom->saveXML();
- //保存XML文件,返回值为int(文件大小,以字节为单位)
- //$dom->save("newfile.xml");
- echo"<hr/>取得所有的title元素:<hr/>";
- $titles=$dom->getElementsByTagName("title");
- foreach($titlesas$node){
- echo$node->textContent."<br/>";
- //这样也可以
- //echo$node->firstChild->data."<br/>";
- }
- /*
- echo"<hr/>从根结点遍历所有结点:<br/>";
- foreach($dom->documentElement->childNodesas$items){
- //如果节点是一个元素(nodeType==1)并且名字是item就继续循环
- if($items->nodeType==1&&$items->nodeName=="item"){
- foreach($items->childNodesas$titles){
- //如果节点是一个元素,并且名字是title就打印它.
- if($titles->nodeType==1&&$titles->nodeName=="title"){
- print$titles->textContent."\n";
- }
- }
- }
- }
- */
- //使用XPath查询数据
- echo"<hr/>使用XPath查询的title节点结果:<hr/>";
- $xpath=newdomxpath($dom);
- $titles=$xpath->query("/rss/channel/item/title");
- foreach($titlesas$node){
- echo$node->textContent."<br/>";
- }
- /*
- 这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多
- 深入一点可能是这样:
- /rss/channel/item[position()=1]/title返回第一个item元素的所有
- /rss/channel/item/title[@id='23']返回所有含有id属性并且值为23的title
- /rss/channel/&folder&/title返回所有articles元素下面的title(译者注:&folder&代表目录深度)
- */
- //向DOM中写入新数据
- $item=$dom->createElement("item");
- $title=$dom->createElement("title");
- $titleText=$dom->createTextNode("titletext");
- $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(newDOMText("This'sthenewtitletext!!!"));
- $firstTitle->parentNode->replaceChild($newTitle,$firstTitle);
- //修改属性
- //$firstTitle=$xpath->query("/rss/channel/item/title")->item(0);
- //$firstTitle->setAttribute("orderby","4");
- $dom->save("newfile.xml");
- echo"<hr/><ahref=\"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文件代码:
- <?xmlversion="1.0"encoding="gb2312"?>
- <rssversion="2.0">
- <channel>
- <title>javascript</title>
- <link>http://blog.youkuaiyun.com/zhongmao/category/29515.aspx</link>
- <description>javascript</description>
- <language>zh-chs</language>
- <generator>.textversion0.958.2004.2001</generator>
- <item>
- <creator>zhongmao</creator>
- <titleorderby="1">outputexcelusedjavascript</title>
- <link>http://blog.youkuaiyun.com/zhongmao/archive/2004/09/15/105385.aspx</link>
- <pubdate>wed,15sep200413:32:00gmt</pubdate>
- <guid>http://blog.youkuaiyun.com/zhongmao/archive/2004/09/15/105385.aspx</guid>
- <comment>http://blog.youkuaiyun.com/zhongmao/comments/105385.aspx</comment>
- <comments>http://blog.youkuaiyun.com/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments>
- <comments>2</comments>
- <commentrss>http://blog.youkuaiyun.com/zhongmao/comments/commentrss/105385.aspx</commentrss>
- <ping>http://blog.youkuaiyun.com/zhongmao/services/trackbacks/105385.aspx</ping>
- <description>testdescription</description>
- </item>
- <item>
- <creator>zhongmao</creator>
- <titleorderby="2">outputwordusedjavascript</title>
- <link>http://blog.youkuaiyun.com/zhongmao/archive/2004/08/06/67161.aspx</link>
- <pubdate>fri,06aug200416:33:00gmt</pubdate>
- <guid>http://blog.youkuaiyun.com/zhongmao/archive/2004/08/06/67161.aspx</guid>
- <comment>http://blog.youkuaiyun.com/zhongmao/comments/67161.aspx</comment>
- <comments>http://blog.youkuaiyun.com/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments>
- <comments>0</comments>
- <commentrss>http://blog.youkuaiyun.com/zhongmao/comments/commentrss/67161.aspx</commentrss>
- <ping>http://blog.youkuaiyun.com/zhongmao/services/trackbacks/67161.aspx</ping>
- <description>testworddescription</description>
- </item>
- <item>
- <creator>zhongmao</creator>
- <titleorderby="3">xmlhttp</title>
- <link>http://blog.youkuaiyun.com/zhongmao/archive/2004/08/02/58417.aspx</link>
- <pubdate>mon,02aug200410:11:00gmt</pubdate>
- <guid>http://blog.youkuaiyun.com/zhongmao/archive/2004/08/02/58417.aspx</guid>
- <comment>http://blog.youkuaiyun.com/zhongmao/comments/58417.aspx</comment>
- <comments>http://blog.youkuaiyun.com/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments>
- <comments>0</comments>
- <commentrss>http://blog.youkuaiyun.com/zhongmao/comments/commentrss/58417.aspx</commentrss>
- <ping>http://blog.youkuaiyun.com/zhongmao/services/trackbacks/58417.aspx</ping>
- <description>xmlhttpaaaasdbbccdd</description>
- </item>
- </channel>
- </rss>