php修改xml結點屬性,增加xml結點屬性的代碼,有需要的朋友可以參考下
php 修改 增加xml結點屬性的代碼,供大家學習參考。
php修改xml結點屬性,增加xml結點屬性的代碼,有需要的朋友,參考下。
1、xml檔案
複製代碼代碼如下:
2、php代碼
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文件屬性並編輯
load('data.xml'); $em=$dom->getElementsByTagName('videos');//最外層節點 $em=$em->item(0); $items=$em->getElementsByTagName('video');//節點 //如果不用讀取直接添加的話把下面這一段去掉即可 foreach($items as $a){ foreach($a->attributes as $b){//$b->nodeValue;節點屬性的值$b->nodeName;節點屬性的名稱 echo $b->nodeName; echo ":"; // www.jbxue.com echo $b->nodeValue; echo "
"; } } //下面是往xml寫入一行新的 $t=$dom->createElement('video');//setAttribute('title','1');//setAttribute('src','2');//setAttribute('img','1');//appendChild($t);// $dom->save('data.xml'); ?>
當時的xml文檔:
//後改的可以修改xml
load('data.xml'); //尋找 videos 節點 $root = $doc->getElementsByTagName('videos'); //第一個 videos 節點 $root = $root->item(0); //尋找 videos 節點下的 video 節點 $userid = $root->getElementsByTagName('video'); //遍曆所有 video 節點 foreach ($userid as $rootdata) { //遍曆每一個 video 節點所有屬性 foreach ($rootdata->attributes as $attrib) { $attribName = $attrib->nodeName; //nodeName為屬性名稱 $attribValue = $attrib->nodeValue; //nodeValue為屬性內容 //尋找屬性名稱為ip的節點內容 if ($attribName =='img') { //尋找屬性內容為ip的節點內容 if ($attribValue =='1') { //將屬性為img,img內容為1的修改為image; $rootdata->setAttribute('img','image'); $doc->save('data.xml'); } } } } ?>
http://www.bkjia.com/PHPjc/625812.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/625812.htmlTechArticlephp修改xml結點屬性,增加xml結點屬性的代碼,有需要的朋友可以參考下 php 修改 增加xml結點屬性的代碼,供大家學習參考。 php修改xml結點屬...