国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php 修改、增加xml結點屬性的實現代碼

2020-03-22 19:27:22
字體:
來源:轉載
供稿:網友
php 修改 增加xml結點屬性的代碼,供大家學習參考。
php修改xml結點屬性,增加xml結點屬性的代碼,有需要的朋友,參考下。1、xml文件復制代碼 代碼如下:
?xml version="1.0" encoding="UTF-8" ?
clientSet
server url="192.168.0.180" port="1935" /
rootPath html' target='_blank'>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代碼

復制代碼 代碼如下:
?
$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文檔屬性并編輯 復制代碼 代碼如下:
?php
//讀取xml
$dom=new DOMDocument('1.0');
$dom- 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 ":";
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');
?

當時的xml文檔:
復制代碼 代碼如下:
?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

//下面這一個文件是后改的可以修改xml 復制代碼 代碼如下:
?php
$doc = new DOMDocument();
$doc- 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');
}
}
}
}
?
PHP教程

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 根河市| 临海市| 潍坊市| 天祝| 太白县| 本溪| 金川县| 宁陕县| 龙海市| 花垣县| 临西县| 漳平市| 息烽县| 兴安县| 西充县| 霍邱县| 绥中县| 大城县| 鹤庆县| 湘潭县| 沂水县| 永泰县| 静乐县| 廉江市| 新乐市| 梓潼县| 南京市| 南雄市| 确山县| 始兴县| 寻乌县| 北辰区| 阿巴嘎旗| 侯马市| 巨野县| 鲁甸县| 辽阳市| 清新县| 宣威市| 惠水县| 宁明县|