天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP5中使用DOM控制XML實現(xiàn)代碼

下面的例子簡單的演示了DOM對XML的操作,詳細解釋請看代碼中的注釋
復(fù)制代碼 代碼如下:
<?
/************************************************
** use XML in php5
** reference site:
** http://cn.php.NET/manual/zh/ref.dom.php
** the follow codes need php5 support
*************************************************/

//首先要創(chuàng)建一個DOMDocument對象
$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é)點遍歷所有結(jié)點:<br/>";
foreach ($dom->documentElement->childNodes as $items) {
//如果節(jié)點是一個元素(nodeType == 1)并且名字是item就繼續(xù)循環(huán)
if ($items->nodeType == 1 && $items->nodeName == "item") {
foreach ($items->childNodes as $titles) {
//如果節(jié)點是一個元素,并且名字是title就打印它.
if ($titles->nodeType == 1 && $titles->nodeName == "title") {
print $titles->textContent . "/n";
}
}
}
}
*/

//使用XPath查詢數(shù)據(jù)
echo "<hr/>使用XPath查詢的title節(jié)點結(jié)果:<hr/>";
$xpath = new domxpath($dom);
$titles = $xpath->query("/rss/channel/item/title");
foreach ($titles as $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中寫入新數(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é)點
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0));
//或者使用xpath查詢出節(jié)點再刪除
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0));
//$dom->save("newfile.xml");

//從DOM中修改節(jié)點數(shù)據(jù)
//修改第一個title的文件
//這個地方比較笨,新創(chuàng)建一個節(jié)點,然后替換舊的節(jié)點。如果哪位朋友有其他好的方法請一定要告訴我
$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元素的內(nèi)容。
/*
$dom->loadHTMLFile("http://www.php.NET/");
$title = $dom->getElementsByTagName("title");
print $title->item(0)->textContent;
*/
?>

下面是test.xml文件代碼:
復(fù)制代碼 代碼如下:
<?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>

php技術(shù)PHP5中使用DOM控制XML實現(xiàn)代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲AVAV天堂AV在线网爱情 | 一区二一二 | 国产精品美女久久久久浪潮AV | 热久久2018亚洲欧美 | yellow日本动漫观看免费 | 久久99精品国产麻豆婷婷 | 乳巨揉みま痴汉电车中文字幕动漫 | 国产亚洲精品久久孕妇呦呦你懂 | 久久国产加勒比精品无码 | 校园全肉高h湿一女多男 | 女人爽到高潮嗷嗷叫视频 | 国产成人免费片在线观看 | 97视频在线观看视频最新 | 久久99re2热在线播放7 | 激情床戏揉胸吃胸视频 | 免费视频精品38 | 欧洲最强rapper潮水喷视频 | 91视频3p| 国产亚洲精品久久久久久白晶晶 | 国产成人亚洲综合无 | 99久久无码一区人妻A片竹菊 | 性色爽爱性色爽爱网站 | 动漫美女禁区 | gay吊粗大双龙 | 桃色窝| 2018国产天天弄谢 | 亚洲熟女丰满多毛XXXXX | 久久精品亚洲AV无码三区观看 | 欧美亚洲另类图片 | 日韩一区二区三区射精 | 99pao成人国产永久免费视频 | 99这里有精品视频视频 | 99久久99久久精品 | 国产亚洲综合视频 | 色欲AV精品人妻一区二区麻豆 | 9966在线观看免费高清电影 | 又硬又粗又大一区二区三区视频 | 小泽玛丽av无码观看 | 日韩 国产 欧美视频二区 | 日韩成人性视频 | 亚洲AV无码乱码在线观看浪潮 |