Home  >  Article  >  Backend Development  >  php 修改、增加xml结点属性的实现代码_PHP

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

WBOY
WBOYOriginal
2016-06-01 11:59:31788browse

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

1、xml文件

复制代码 代码如下:





















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文档属性并编辑

复制代码 代码如下:
//读取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 "
";
}
}
//下面是往xml写入一行新的
$t=$dom->createElement('video');//

复制代码 代码如下:
$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');
}
}
}

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn