Php modifies the xml node attribute and adds the code for adding the xml node attribute. if you need it, you can refer to the php code for modifying and adding the xml node attribute for your reference.
Php modifies xml node attributes and adds code for xml node attributes. For more information, see.
1. xml file
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');?>
PHP parses XML document attributes and edits
Load ('data. xml '); $ em = $ dom-> getElementsByTagName ('videos'); // The outermost node $ em = $ em-> item (0 ); $ items = $ em-> getElementsByTagName ('video'); // node // If you do not need to read it, remove the following section to foreach ($ items as $) {foreach ($ a-> attributes as $ B) {// $ B-> nodeValue; node attribute value $ B-> nodeName; node attribute name echo $ B-> nodeName; echo ":"; echo $ B-> nodeValue; echo"
";}}// Write a new $ t = $ dom-> createElement ('video') to xml ');//
SetAttribute ('title', '1 ');//
SetAttribute ('src', '2 ');//
SetAttribute ('IMG ', '1 ');//
AppendChild ($ t );//
$ Dom-> save ('data. XML');?>
The xml document at that time:
// The following file is modified later. you can modify the xml file.
Load ('data. XML'); // find the videos node $ root = $ doc-> getElementsByTagName ('videoos '); // The first videos node $ root = $ root-> item (0 ); // find the video node under the videos node $ userid = $ root-> getElementsByTagName ('video'); // traverse all video nodes foreach ($ userid as $ rootdata) {// traverse all attributes of each video node foreach ($ rootdata-> attributes as $ attrib) {$ attribName = $ attrib-> nodeName; // nodeName is the attribute name $ attribValue = $ attrib-> nodeValue; // nodeVal Ue is the property content // find the node content whose property name is ip if ($ attribName = 'IMG ') {// if ($ attribValue = '1') {// modify the node content whose attribute is "img" and "img content is" 1 "to" image; $ rootdata-> setAttribute ('IMG ', 'image'); $ doc-> save ('data. XML') ;}}}?>
For more articles about php code modification and adding xml node attributes, please follow the PHP Chinese website!