首页 > 代码库 > php 修改、增加xml结点属性的实现代码

php 修改、增加xml结点属性的实现代码

php修改xml结点属性,增加xml结点属性的代码,有需要的朋友可以参考下

php 修改 增加xml结点属性的代码,供大家学习参考。
php修改xml结点属性,增加xml结点属性的代码,有需要的朋友,参考下。

1、xml文件

 复制代码代码如下:

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

  

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 ":";  // www.jbxue.com 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="http://www.mamicode.com/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="http://www.mamicode.com/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 = http://www.mamicode.com/$attrib->nodeValue; //nodeValue为属性内容 //查找属性名称为ip的节点内容 if ($attribName ==img) { //查找属性内容为ip的节点内容 if ($attribValue =http://www.mamicode.com/=1) { //将属性为img,img内容为1的修改为image; $rootdata->setAttribute(img,image); $doc->save(data.xml); } } } }  ?>

 

php 修改、增加xml结点属性的实现代码