php如何删除xml节点-PHP问题

资源魔 96 0

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 示例
<?xml version="1.0" encoding="UTF-8"?>
<birdList>
<bird>
<speciesID>1</speciesID>
<localName>雪鹑</localName>
<engName>Snow Partridge</engName>
<latinName>Lerwa lerwa</latinName>
</bird>
<bird>
<speciesID>2</speciesID>
<localName>藏雪鸡</localName>
<engName>Tibetan Snowcock</engName>
<latinName>Tetraogallus tibetanus</latinName>
</bird>
</birdList>

更多相干常识,请拜访PHP中文网!

以上就是php若何删除了xml节点的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题 xml节点

抱歉,评论功能暂时关闭!