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

yipeiwu_com6年前PHP代码库

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('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取整数函数常用的四种方法小结

ceil — 进一法取整 说明 float ceil ( float value ) 返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍...

利用swoole+redis实现股票和区块链服务

本文主要给大家介绍了关于swoole+redis实现股票和区块链服务的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 PHP 的redis扩展是阻塞式 IO...

PHP的自定义模板引擎

PHP的自定义模板引擎

前面的话 在大多数的项目组中,开发一个Web程序都会出现这样的流程:计划文档提交之后,前端工程师制作了网站的外观模型,然后把它交给后端工程师,它们使用后端代码实现程序逻辑,同时使用外观模...

PHP的命令行命令使用指南

 当用户打开php页面时,服务端便执行PHP的命令并将执行结果发送至用户的浏览器中,这类似于ASP和CoildFusion,PHP可以运行在WINDOWS和多种版本的UNIX上...

PHP运行出现Notice : Use of undefined constant 的完美解决方案分享

Notice: Use of undefined constant ALL_PS - assumed 'ALL_PS' in E:\Server\vhosts\www.lvtao.net...