Maison  >  Article  >  développement back-end  >  php 修改、增加xml结点属性的实现代码

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

高洛峰
高洛峰original
2017-01-11 13:21:381901parcourir

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="" />
<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(&#39;1.0&#39;);
$dom->load(&#39;x.xml&#39;);
$em=$dom->getElementsByTagName(&#39;emotions&#39;);
$em=$em->item(0);
$items=$em->getElementsByTagName(&#39;item&#39;);
foreach($items as $a){
foreach($a->attributes as $b){
if($b->nodeValue==&#39;Birthday&#39;){
$a->setAttribute(&#39;name&#39;,&#39;nBirthday&#39;);
}
}
}
$t=$dom->createElement(&#39;item&#39;);
$t->setAttribute(&#39;name&#39;,&#39;x&#39;);
$t->setAttribute(&#39;src&#39;,&#39;www.baidu.com&#39;);
$t->setAttribute(&#39;duration&#39;,&#39;duration&#39;);
$em->appendChild($t);
$dom->save(&#39;x.xml&#39;);
?>

PHP解析XML文档属性并编辑

<?php 
//读取xml 
 $dom=new DOMDocument(&#39;1.0&#39;); 
$dom->load(&#39;data.xml&#39;); 
$em=$dom->getElementsByTagName(&#39;videos&#39;);//最外层节点 
$em=$em->item(0); 
$items=$em->getElementsByTagName(&#39;video&#39;);//节点 
//如果不用读取直接添加的话把下面这一段去掉即可 
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(&#39;video&#39;);//<video 
$t->setAttribute(&#39;title&#39;,&#39;1&#39;);//<video name="data" 
$t->setAttribute(&#39;src&#39;,&#39;2&#39;);//<video name="data" src="2" 
$t->setAttribute(&#39;img&#39;,&#39;1&#39;);//<video name="data" img="1" 
$em->appendChild($t);//<video name="data" img="1"/> 
$dom->save(&#39;data.xml&#39;); 
?>

当时的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(&#39;data.xml&#39;); 

//查找 videos 节点 
$root = $doc->getElementsByTagName(&#39;videos&#39;); 

//第一个 videos 节点 
$root = $root->item(0); 

//查找 videos 节点下的 video 节点 
$userid = $root->getElementsByTagName(&#39;video&#39;); 

//遍历所有 video 节点 
foreach ($userid as $rootdata) 
{ 
//遍历每一个 video 节点所有属性 
foreach ($rootdata->attributes as $attrib) 
{ 
$attribName = $attrib->nodeName;   //nodeName为属性名称 
$attribValue = $attrib->nodeValue; //nodeValue为属性内容 

//查找属性名称为ip的节点内容 
if ($attribName ==&#39;img&#39;) 
{ 
//查找属性内容为ip的节点内容 
if ($attribValue ==&#39;1&#39;) 
{ 
//将属性为img,img内容为1的修改为image; 
$rootdata->setAttribute(&#39;img&#39;,&#39;image&#39;); 
$doc->save(&#39;data.xml&#39;); 
} 
} 
} 
}  
?>

更多php 修改、增加xml结点属性的实现代码相关文章请关注PHP中文网!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Java中构造、生成XML简明教程Article suivant:java生成XML的方法