Ask an XML related question

Source: Internet
Author: User
Tags xpath
Ask an XML question
This is an IP security policy XML that means that each user can only be in IP for Inside content can be logged in, I want to know how to modify and delete a user's IP list, I use DOMDOCUMENT ();




192.168.1.1
192.168.1.2 //For example, I would like to modify or delete the admin ip2, the other one remains the same
192.168.1.3 //Because the node name is the same, I'm not here to add ID
192.168.1.4
192.168.1.5


192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15


192.168.1.21
192.168.1.22
192.168.1.23
192.168.1.24
192.168.1.25



------Solution--------------------
Tragedy, tragedy. DOMDocument is a very standard DOM model, PHP manual detailed methods and examples, why lazy?
------Solution--------------------
PHP Code
   
   
     
      
      
        192.168.1.1 
       
      
        192.168.1.2 
       
      
        192.168.1.3 
       
      
        192.168.1.4 
       
      
        192.168.1.5 
       
      
      
      
        192.168.1.11 
       
      
        192.168.1.12 
       
      
        192.168.1.13 
       
      
        192.168.1.14 
       
      
        192.168.1.15 
        
       
       
         192.168.1.21 
        
       
         192.168.1.22 
        
       
         192.168.1.23 
       

       
         192.168.1.24 
        
       
         192.168.1.25 
        
       
     
    
   EOF, $dom = new DOMDocument (), $ret = $dom->loadxml ($xml, libxml_noblanks), $xpath = new Domxpath ($dom); $node _list = $xpa Th->query ('/user/username[@name = ' admin ']/iplist '); for ($i = 0; $i! = $node _list->length; + + $i) {echo $node _ List->item ($i)->nodevalue. Php_eol;}? >
------Solution--------------------
PHP Code
$s =<<< XML
     
     
       
        
        
          192.168.1.1 
         
        
          192.168.1.2 
         
        
          192.168.1.3 
         
        
          192.168.1.4 
         
        
          192.168.1.5 
         
        
        
        
          192.168.1.11 
         
        
          192.168.1.12 
         
        
          192.168.1.13 
         
        
          192.168.1.14 
         
        
          192.168.1.15 
          
         
         
           192.168.1.21 
          
         
           192.168.1.22 
          
         
           192.168.1.23 
          
         
           192.168.1.24 
          
         
           192.168.1.25 
          
         
       
      
     XML; $dom = new DomDocument; $dom->loadxml ($s); $xpath = new Domxpath ($dom);//Use XPath to position modify $p = $xpath->query ("// username[@name = ' admin ']/iplist[.= ' 192.168.1.1 ')->item (0), $p->nodevalue = ' ABCD ';//Use XPath to locate delete $p = $xpath- >query ("//username[@name = ' admin ']/iplist[.= ' 192.168.1.2 ']")->item (0); $p->parentnode->removechild ($ p); Echo $dom->savexml ();
  • 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.