php删除xml节点的方法:首先遍历所有bird节点以及每一个bird节点的所有属性;然后修改节点值;最后通过“removeChild”方法删除节点即可。
php xml读取、编辑、删除节点
代码如下:$doc = new DOMDocument();
$doc->load('bird_base.xml');
$root = $doc->getElementsByTagName('birdList');
$root = $root->item(0);
$bird = $root->getElementsByTagName('bird');
//遍历所有 bird 节点
foreach ($bird as $rootdata) {
//遍历每一个 bird 节点所有属性
$speciesID= $rootdata->getElementsByTagName('speciesID')->item(0)->nodeValue;
$localName= $rootdata->getElementsByTagName('localName')->item(0)->nodeValue;
$engName= $rootdata->getElementsByTagName('engName')->item(0)->nodeValue;
$latinName= $rootdata->getElementsByTagName('latinName')->item(0)->nodeValue;
$bird = $this->bird->get_bird_detail($speciesID);
if(!empty($bird))
{
//修改节点值
$rootdata->getElementsByTagName('localName')->item(0)->nodeValue = $bird['localName'];
$rootdata->getElementsByTagName('engName')->item(0)->nodeValue = $bird['engName'];
$rootdata->getElementsByTagName('latinName')->item(0)->nodeValue = $bird['latinName'];
}else
{
//删除节点
$rootdata->parentNode->removeChild($rootdata);
}
}
$doc->save('bird_base.xml');
bird_base.xml 示例
1
雪鹑
Snow Partridge
Lerwa lerwa
2
藏雪鸡
Tibetan Snowcock
Tetraogallus tibetanus
更多相关知识,请访问PHP中文网!