PHP 修改 增加xml结点属性的代码,供大家学习参考。
PHP修改xml结点属性,增加xml结点属性的代码,有需要的朋友,参考下。
1、xml文件
代码如下:@H_403_7@
@H_403_7@
@H_403_7@
代码如下:@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@
$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');//
//读取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');//