Php code for modifying and adding xml node attributes

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.