时间:2021-07-01 10:21:17 帮助过:11人阅读
EOF; $dom = new DOMDocument(); $ret = $dom->loadXML($xml, LIBXML_NOBLANKS); $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; } ?> 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
------解决方案--------------------
$s =<<< XMLXML; $dom = new domDocument; $dom->loadXML($s); $xpath = new DOMXPath($dom); //使用xpath定位修改 $p = $xpath->query("//username[@name='admin']/iplist[.='192.168.1.1']")->item(0); $p->nodeValue = 'abcd'; //使用xpath定位删除 $p = $xpath->query("//username[@name='admin']/iplist[.='192.168.1.2']")->item(0); $p->parentNode->removeChild($p); echo $dom->saveXML(); 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