php如何修改、增加xml結(jié)點(diǎn)屬性-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)php如何修改、增加xml結(jié)點(diǎn)屬性,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

溫江網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,溫江網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為溫江上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個(gè)售后服務(wù)好的溫江做網(wǎng)站的公司定做!

1、xml文件

代碼如下:


<?xml version="1.0" encoding="UTF-8" ?>
<clientSet>
<server url="192.168.0.180" port="1935" />
<rootPath value="" />
<homePath value="http://www.aaa.com" />
<helpPath value="help.html" />
<language value="en" />
<theme value="default" />
<visibleMarquee value = "true" />
<visibleWhitePaper value="true" />
<showMemberRoomForGuest value = "true" />
<emotions enabled="true" column="5" autoPlay="false">
<item name="Birthday" src="cartoon/movie/birthday.swf" thumb="cartoon/preview/birthday-small.swf" duration="15"/>
<item name="Boom" src="cartoon/movie/boom.swf" thumb="cartoon/preview/boom-small.swf" duration="6"/>
<item name="Bubble" src="cartoon/movie/bubble.swf" thumb="cartoon/preview/bubble-small.swf" duration="7.5"/>
<item name="Cry" src="cartoon/movie/cry.swf" thumb="cartoon/preview/cry-small.swf" duration="5.4"/>
<item name="Doggie" src="cartoon/movie/doggie.swf" thumb="cartoon/preview/doggie-small.swf" duration="13"/>
<item name="Greeting" src="cartoon/movie/greeting.swf" thumb="cartoon/preview/greeting-small.swf" duration="7.4"/>
<item name="Football" src="cartoon/movie/football.swf" thumb="cartoon/preview/football-small.swf" duration="2.2"/>
</emotions >
</clientSet>


2、php代碼



復(fù)制代碼 代碼如下:


<?
$dom=new DOMDocument('1.0');
$dom->load('x.xml');
$em=$dom->getElementsByTagName('emotions');
$em=$em->item(0);
$items=$em->getElementsByTagName('item');
foreach($items as $a){
foreach($a->attributes as $b){
if($b->nodeValue=='Birthday'){
$a->setAttribute('name','nBirthday');
}
}
}
$t=$dom->createElement('item');
$t->setAttribute('name','x');
$t->setAttribute('src','www.baidu.com');
$t->setAttribute('duration','duration');
$em->appendChild($t);
$dom->save('x.xml');
?>


PHP解析XML文檔屬性并編輯

復(fù)制代碼 代碼如下:


<?php
//讀取xml
 $dom=new DOMDocument('1.0');
$dom->load('data.xml');
$em=$dom->getElementsByTagName('videos');//最外層節(jié)點(diǎn)
$em=$em->item(0);
$items=$em->getElementsByTagName('video');//節(jié)點(diǎn)
//如果不用讀取直接添加的話把下面這一段去掉即可
foreach($items as $a){
foreach($a->attributes as $b){//$b->nodeValue;節(jié)點(diǎn)屬性的值$b->nodeName;節(jié)點(diǎn)屬性的名稱
 echo $b->nodeName;
 echo ":";
 echo $b->nodeValue;
 echo "<br/>";
}
}
//下面是往xml寫入一行新的
$t=$dom->createElement('video');//<video
$t->setAttribute('title','1');//<video name="data"
$t->setAttribute('src','2');//<video name="data" src="2"
$t->setAttribute('img','1');//<video name="data" img="1"
$em->appendChild($t);//<video name="data" img="1"/>
$dom->save('data.xml');
?> 


當(dāng)時(shí)的xml文檔:

復(fù)制代碼 代碼如下:


<?xml version="1.0"?>
<videos>
 <video img="a" url="1" title="1" nickname="1" tag="1" vid="1" star="1"/>
 <video img="b" url="2" title="2" nickname="2" tag="2" vid="2" star="2"/>
 <video img="c" url="3" title="3" nickname="3" tag="3" vid="3" star="3"/>
 <video title="d" src="2" img="1"/>
</videos>



//下面這一個(gè)文件是后改的可以修改xml

復(fù)制代碼 代碼如下:


<?php
$doc = new DOMDocument();
$doc->load('data.xml');

//查找 videos 節(jié)點(diǎn)
$root = $doc->getElementsByTagName('videos');

//第一個(gè) videos 節(jié)點(diǎn)
$root = $root->item(0);

//查找 videos 節(jié)點(diǎn)下的 video 節(jié)點(diǎn)
$userid = $root->getElementsByTagName('video');

//遍歷所有 video 節(jié)點(diǎn)
foreach ($userid as $rootdata)
{
//遍歷每一個(gè) video 節(jié)點(diǎn)所有屬性
foreach ($rootdata->attributes as $attrib)
{
$attribName = $attrib->nodeName;   //nodeName為屬性名稱
$attribValue = $attrib->nodeValue; //nodeValue為屬性內(nèi)容

//查找屬性名稱為ip的節(jié)點(diǎn)內(nèi)容
if ($attribName =='img')
{
//查找屬性內(nèi)容為ip的節(jié)點(diǎn)內(nèi)容
if ($attribValue =='1')
{
//將屬性為img,img內(nèi)容為1的修改為image;
$rootdata->setAttribute('img','image');
$doc->save('data.xml');
}
}
}

?>


關(guān)于“php如何修改、增加xml結(jié)點(diǎn)屬性”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網(wǎng)頁名稱:php如何修改、增加xml結(jié)點(diǎn)屬性-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.muchs.cn/article4/dsscoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、建站公司、云服務(wù)器網(wǎng)站制作、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站