PHP修改和增加XML节点属性的实现代码
以下提供使用PHP修改及增加XML节点属性的参考代码,供开发者学习借鉴。
首先准备一个XML文件作为操作对象。
具体实现代码如下:
接下来是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内容。
相关代码片段如下: