Ask an XML question. this is the XML of an IP security policy, meaning that each user can log on only when the ip address is & lt; iplist & gt, I want to know how to modify and delete a user's ip list. I use DOMDOCUMENT (); & lt ;? Xmlversi ask an XML question
This is the XML of an IP security policy, meaning that each user can only You can log on only when the content is in. I want to know how to modify and delete the ip address list of a user. I use DOMDOCUMENT ();
192.168.1.1
192.168.1.2 // For example, if I want to modify or delete admin ip2, the rest will remain unchanged.
192.168.1.3 // Because the node names are the same, do you want to give 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 and tragedy. DomDocument is a very standard DOM model. php manual provides detailed methods and examples. why is it 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_noblks); $ xpath = new DOMXpath ($ dom ); $ node_list = $ xpath-> 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 locate and modify $ p = $ xpath-> query ("// username [@ name = 'admin']/iplist [. = '2017. 168.1.1 '] ")-> item (0); $ p-> nodeValue = 'abc '; // use xpath to locate and delete $ p = $ xpath-> query ("// username [@ name = 'admin']/iplist [. = '2017. 168.1.2 '] ")-> item (0); $ p-> parentNode-> removeChild ($ p); echo $ dom-> saveXML ();