Question about modifying xml file values

Source: Internet
Author: User
To modify the xml file value, I want to change the value of question to 22222. this value is passed in at the front end, but I don't know how to transfer it. how should I do it? It is best to use the DOMDocument class for operations.
 
   
  3  admin  
      
         
    
     111111
          
            
     
      A
             
     
      11
                 
            
     
      B
             
     
      22
                 
            
     
      C
             
     
      33
                 
            
     
      D
             
     
      44
               
       
         
    
     22222
          
            
     
      A
             
     
      22
                 
            
     
      B
             
     
      11
                 
            
     
      C
             
     
      44
                 
            
     
      D
             
     
      33
               
       
         
    
     33333
          
            
     
      A
             
     
      444
                 
            
     
      B
             
     
      222
                 
            
     
      C
             
     
      333
                 
            
     
      D
             
     
      2111
               
     
  
 


Reply to discussion (solution)

How do you change it?

$ Fn = 'Your xml filename '; $ tag = 'question'; // The name of the node to be modified $ val = '000000'; // The value of the node to be modified, used together with $ tag to locate $ replace = 'XXX'; // The modified value $ xml = simplexml_load_file ($ fn ); $ p = $ xml-> xpath ("// {$ tag }[. = '$ val'] "); $ p [0] [0] = $ replace; $ xml-> asXML ($ fn); // write back

PHP code? 12345678 $ fn = 'Your xml filename '; $ tag = 'question'; // The name of the node to be modified $ val = '000000'; // The value of the node to be modified, used together with $ tag to locate $ replace = 'XXX'; // The modified value $ xml = simplexml_load_file ($ fn ); $ p = $ xml-> xpath ("// {$ tag }[. = ......

$ P [0] [0] what does it mean?

Xpath returns an array of all conforming nodes (a two-dimensional array of objects)

$ P [0] [0] indicates the first matched node
$ P [0] [0] indicates the first element in the node.

Xpath returns an array of all conforming nodes (a two-dimensional array of objects)

$ P [0] [0] indicates the first matched node
$ P [0] [0] indicates the first element in the node.
In this case, the question is 111111, not 222222.

$ Xml-> xpath ("// {$ tag} [. = '$ VAL']");
The actual execution is
$ Xml-> xpath ("// question [. = '000000']");

Therefore, your data will only match 22222

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.