隨筆 - 8  文章 - 55  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          朋友的Blog

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          PHP5中增強(qiáng)了XML的支持,使用DOM擴(kuò)展了XML操作的能耐。這些函數(shù)作為 PHP5 核心的一部分,無(wú)需被安裝即可使用。
          ??
          ??下面的例子簡(jiǎn)單的演示了DOM對(duì)XML的操作,詳細(xì)解釋請(qǐng)看代碼中的注釋
          ??
          ??<?
          ??/************************************************
          ??** use XML in PHP5
          ??** reference site:
          ??** http://cn.php.net/manual/zh/ref.dom.php
          ??** the follow codes need PHP5 support
          ??** www.knowsky.com
          ??*************************************************/
          ??
          ??
          ??//首先要?jiǎng)?chuàng)建一個(gè)DOMDocument對(duì)象
          ??$dom = new DomDocument();
          ??//然后載入XML文件
          ??$dom -> load("test.xml");
          ??
          ??//輸出XML文件
          ??//header("Content-type: text/xml;charset=gb2312");
          ??//echo $dom -> saveXML();
          ??
          ??//保存XML文件,返回值為int(文件大小,以字節(jié)為單位)
          ??//$dom -> save("newfile.xml");
          ??
          ??echo "<hr/>取得所有的title元素:<hr/>";
          ??$titles = $dom -> getElementsByTagName("title");
          ??foreach ($titles as $node)
          ??{
          ?? echo $node -> textContent . "<br/>";
          ?? //這樣也可以
          ?? //echo $node->firstChild->data . "<br/>";
          ??}
          ??
          ??/*
          ??echo "<hr/>從根結(jié)點(diǎn)遍歷所有結(jié)點(diǎn):<br/>";
          ??foreach ($dom->documentElement->childNodes as $items) {
          ?? //如果節(jié)點(diǎn)是一個(gè)元素(nodeType == 1)并且名字是item就繼續(xù)循環(huán)
          ?? if ($items->nodeType == 1 && $items->nodeName == "item") {
          ?? foreach ($items->childNodes as $titles) {
          ?? //如果節(jié)點(diǎn)是一個(gè)元素,并且名字是title就打印它.
          ?? if ($titles->nodeType == 1 && $titles->nodeName == "title") {
          ?? print $titles->textContent . "\n";
          ?? }
          ?? }
          ?? }
          ??}
          ??*/
          ??
          ??//使用XPath查詢數(shù)據(jù)
          ??echo "<hr/>使用XPath查詢的title節(jié)點(diǎn)結(jié)果:<hr/>";
          ??$xpath = new domxpath($dom);
          ??$titles = $xpath->query("/rss/channel/item/title");
          ??foreach ($titles as $node)
          ??{
          ?? echo $node->textContent."<br/>";
          ??}
          ??/*
          ??這樣和使用getElementsByTagName()方法差不多,但是Xpath要強(qiáng)大的多
          ??深入一點(diǎn)可能是這樣:
          ??/rss/channel/item[position() = 1]/title 返回第一個(gè)item元素的所有
          ??/rss/channel/item/title[@id = '23'] 返回所有含有id屬性并且值為23的title
          ??/rss/channel/&folder&/title 返回所有articles元素下面的title(譯者注:&folder&代表目錄深度)
          ??*/


          //向DOM中寫入新數(shù)據(jù)
          ??$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中刪除節(jié)點(diǎn)
          ??//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0));
          ??//或者使用xpath查詢出節(jié)點(diǎn)再刪除
          ??//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0));
          ??//$dom->save("newfile.xml");
          ??
          ??//從DOM中修改節(jié)點(diǎn)數(shù)據(jù)
          ??//修改第一個(gè)title的文件
          ??//這個(gè)地方比較笨,新創(chuàng)建一個(gè)節(jié)點(diǎn),然后替換舊的節(jié)點(diǎn)。如果哪位朋友有其他好的方法請(qǐng)一定要告訴我
          ??$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的首頁(yè),將返第一個(gè)title元素的內(nèi)容。
          ??/*
          ??$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>
          posted on 2006-10-10 09:53 blog搬家了--[www.ialway.com/blog] 閱讀(239) 評(píng)論(0)  編輯  收藏 所屬分類: PHP
          主站蜘蛛池模板: 扎鲁特旗| 枣强县| 南投县| 通州区| 同心县| 汨罗市| 祥云县| 衡东县| 乌拉特中旗| 突泉县| 齐齐哈尔市| 于田县| 射阳县| 乌拉特前旗| 个旧市| 宁南县| 山阳县| 三穗县| 罗平县| 梅州市| 松滋市| 柘荣县| 闽侯县| 尚义县| 岫岩| 宜章县| 井冈山市| 黔南| 定结县| 黄山市| 安宁市| 南汇区| 蓝山县| 塔城市| 瑞金市| 金沙县| 玛曲县| 奉贤区| 赣榆县| 湖北省| 华容县|