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 ();