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

前端之家收集整理的这篇文章主要介绍了php 修改、增加xml结点属性的实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

1、xml文件

代码如下:@H_403_7@













@H_403_7@

2、PHP代码

代码如下:@H_403_7@
$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');
?>
@H_403_7@

PHP解析XML文档属性并编辑

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