|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为 PHP5 核心的一部分,无需被安装即可使用。 RZaLinux联盟 RZaLinux联盟 下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释 RZaLinux联盟 RZaLinux联盟 <? RZaLinux联盟 /************************************************ RZaLinux联盟 ** use XML in PHP5 RZaLinux联盟 ** reference site: RZaLinux联盟 ** http://cn.php.net/manual/zh/ref.dom.php RZaLinux联盟 ** the follow codes need PHP5 support RZaLinux联盟 ** RZaLinux联盟 *************************************************/ RZaLinux联盟 RZaLinux联盟 RZaLinux联盟 //首先要创建一个DOMDocument对象 RZaLinux联盟 $dom = new DomDocument(); RZaLinux联盟 //然后载入XML文件 RZaLinux联盟 $dom -> load("test.xml"); RZaLinux联盟 RZaLinux联盟 //输出XML文件 RZaLinux联盟 //header("Content-type: text/xml;charset=gb2312"); RZaLinux联盟 //echo $dom -> saveXML(); RZaLinux联盟 RZaLinux联盟 //保存XML文件,返回值为int(文件大小,以字节为单位) RZaLinux联盟 //$dom -> save("newfile.xml"); RZaLinux联盟 RZaLinux联盟 echo "<hr/>取得所有的title元素:<hr/>"; RZaLinux联盟 $titles = $dom -> getElementsByTagName("title"); RZaLinux联盟 foreach ($titles as $node) RZaLinux联盟 { RZaLinux联盟 echo $node -> textContent . "<br/>"; RZaLinux联盟 //这样也可以 RZaLinux联盟 //echo $node->firstChild->data . "<br/>"; RZaLinux联盟 } RZaLinux联盟 RZaLinux联盟 /* RZaLinux联盟 echo "<hr/>从根结点遍历所有结点:<br/>"; RZaLinux联盟 foreach ($dom->documentElement->childNodes as $items) { RZaLinux联盟 //如果节点是一个元素(nodeType == 1)并且名字是item就继续循环 RZaLinux联盟 if ($items->nodeType == 1 && $items->nodeName == "item") { RZaLinux联盟 foreach ($items->childNodes as $titles) { RZaLinux联盟 //如果节点是一个元素,并且名字是title就打印它. RZaLinux联盟 if ($titles->nodeType == 1 && $titles->nodeName == "title") { RZaLinux联盟 print $titles->textContent . "\n"; RZaLinux联盟 } RZaLinux联盟 } RZaLinux联盟 } RZaLinux联盟 } RZaLinux联盟 */ RZaLinux联盟 RZaLinux联盟 //使用XPath查询数据 RZaLinux联盟 echo "<hr/>使用XPath查询的title节点结果:<hr/>"; RZaLinux联盟 $xpath = new domxpath($dom); RZaLinux联盟 $titles = $xpath->query("/rss/channel/item/title"); RZaLinux联盟 foreach ($titles as $node) RZaLinux联盟 { RZaLinux联盟 echo $node->textContent."<br/>"; RZaLinux联盟 } RZaLinux联盟 /* RZaLinux联盟 这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多 RZaLinux联盟 深入一点可能是这样: RZaLinux联盟 /rss/channel/item[position() = 1]/title 返回第一个item元素的所有 RZaLinux联盟 /rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title RZaLinux联盟 /rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度) RZaLinux联盟 */ RZaLinux联盟 RZaLinux联盟 RZaLinux联盟 //向DOM中写入新数据 RZaLinux联盟 $item = $dom->createElement("item"); RZaLinux联盟 $title = $dom->createElement("title"); RZaLinux联盟 $titleText = $dom->createTextNode("title text"); RZaLinux联盟 $title->appendChild($titleText); RZaLinux联盟 $item->appendChild($title); RZaLinux联盟 $dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item); RZaLinux联盟 RZaLinux联盟 //从DOM中删除节点 RZaLinux联盟 //$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0)); RZaLinux联盟 //或者使用xpath查询出节点再删除 RZaLinux联盟 //$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0)); RZaLinux联盟 //$dom->save("newfile.xml"); RZaLinux联盟 RZaLinux联盟 //从DOM中修改节点数据 RZaLinux联盟 //修改第一个title的文件 RZaLinux联盟 //这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我 RZaLinux联盟 $firstTitle = $xpath->query("/rss/channel/item/title")->item(0); RZaLinux联盟 $newTitle = $dom->createElement("title"); RZaLinux联盟 $newTitle->appendChild(new DOMText("This's the new title text!!!")); RZaLinux联盟 $firstTitle->parentNode->replaceChild($newTitle, $firstTitle); RZaLinux联盟 //修改属性 RZaLinux联盟 //$firstTitle = $xpath->query("/rss/channel/item/title")->item(0); RZaLinux联盟 //$firstTitle->setAttribute("orderby", "4"); RZaLinux联盟 $dom->save("newfile.xml"); RZaLinux联盟 RZaLinux联盟 echo "<hr/><a href=\"newfile.xml\">查看newfile.xml</a>"; RZaLinux联盟 RZaLinux联盟 //下面的代码获得并解析php.net的首页,将返第一个title元素的内容。 RZaLinux联盟 /* RZaLinux联盟 $dom->loadHTMLFile("http://www.php.net/"); RZaLinux联盟 $title = $dom->getElementsByTagName("title"); RZaLinux联盟 print $title->item(0)->textContent; RZaLinux联盟 */ RZaLinux联盟 ?> RZaLinux联盟 RZaLinux联盟 下面是test.xml文件代码: RZaLinux联盟 RZaLinux联盟 <?xml version="1.0" encoding="gb2312"?> RZaLinux联盟 <rss version="2.0"> RZaLinux联盟 <channel> RZaLinux联盟 <title>javascript</title> RZaLinux联盟 <link>http://blog.csdn.net/zhongmao/category/29515.aspx</link> RZaLinux联盟 <description>javascript</description> RZaLinux联盟 <language>zh-chs</language> RZaLinux联盟 <generator>.text version 0.958.2004.2001</generator> RZaLinux联盟 <item> RZaLinux联盟 <creator>zhongmao</creator> RZaLinux联盟 <title orderby="1">out put Excel used javascript</title> RZaLinux联盟 <link>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</link> RZaLinux联盟 <pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate> RZaLinux联盟 <guid>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx</guid> RZaLinux联盟 <comment>http://blog.csdn.net/zhongmao/comments/105385.aspx</comment> RZaLinux联盟 <comments>http://blog.csdn.net/zhongmao/archive/2004/09/15/105385.aspx#feedbackRZaLinux联盟 </comments> RZaLinux联盟 <comments>2</comments> RZaLinux联盟 <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/105385.aspxRZaLinux联盟 </commentrss> RZaLinux联盟 <ping>http://blog.csdn.net/zhongmao/services/trackbacks/105385.aspx</ping> RZaLinux联盟 <description>test description</description> RZaLinux联盟 </item> RZaLinux联盟 <item> RZaLinux联盟 <creator>zhongmao</creator> RZaLinux联盟 <title orderby="2">out put word used javascript</title> RZaLinux联盟 <link>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</link> RZaLinux联盟 <pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate> RZaLinux联盟 <guid>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx</guid> RZaLinux联盟 <comment>http://blog.csdn.net/zhongmao/comments/67161.aspx</comment> RZaLinux联盟 <comments>http://blog.csdn.net/zhongmao/archive/2004/08/06/67161.aspx#feedbackRZaLinux联盟 </comments> RZaLinux联盟 <comments>0</comments> RZaLinux联盟 <commentrss>http://blog.csdn.net/zhongmao/comments/commentrss/67161.aspxRZaLinux联盟 </commentrss> RZaLinux联盟 <ping>http://blog.csdn.net/zhongmao/services/trackbacks/67161.aspx</ping> RZaLinux联盟 <description>test word description</description> RZaLinux联盟 </item> RZaLinux联盟 <item> RZaLinux联盟 <creator>zhongmao</creator> RZaLinux联盟 <title orderby="3">xmlhttp</title> RZaLinux联盟 <link>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</link> RZaLinux联盟 <pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate> RZaLinux联盟 <guid>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx</guid> RZaLinux联盟 <comment>http://blog.csdn.net/zhongmao/comments/58417.aspx</comment> RZaLinux联盟 <comments>http://blog.csdn.net/zhongmao/archive/2004/08/02/58417.aspx#feedbackRZaLinux联盟 </comments> RZaLinux联盟 <comments>0</comments> RZaLinux联盟 <commentrss>http://blog.csdn.net/zhongmao/comments/commentrssRZaLinux联盟 /58417.aspx</commentrss> RZaLinux联盟 <ping>http://blog.csdn.net/zhongmao/services/trackbacks/58417.aspx</ping> RZaLinux联盟 <description>xmlhttpaaa asd bb cc dd</description> RZaLinux联盟 </item> RZaLinux联盟 </channel> RZaLinux联盟 </rss> RZaLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|