PHP implementation code for modifying and adding xml node attributes

高洛峰
Release: 2017-01-11 13:21:38
Original
1894 people have browsed it

php Modify the code to add xml node attributes for your reference.
php modifies xml node attributes and adds the code for xml node attributes. Friends in need can refer to it.

1.xml file

                    
Copy after login

2.php code

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'); ?>
Copy after login

PHP parses the XML document attributes and edits

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');//
Copy after login

The xml document at that time:

  
Copy after login

//The following file was modified later and can be modified xml

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'); } } } } ?>
Copy after login

For more PHP implementation codes for modifying and adding xml node attributes, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!