通过php删除xml节点和所有子节点

时间:2013-04-22 17:37:42

标签: php xml domdocument removechild

我一直在这里和谷歌一起寻找并找到各种代码,但它们都不起作用。我假设它是因为它们都不符合我的确切情况。我有一个像这样的xml结构:

<employees>
        <employee EmpID="">
            <first_name></first_name>
            <last_name></last_name>
            <ssnum></ssnum>
            <status></status>
            <contact_info>
                    <office_phone></office_phone>
                    <email></email>
                    <cell_phone></cell_phone>
            </contact_info>
            <access_info level="user">
                    <username></username>
                    <password></password>
            </access_info>
            <department></department>
            <date_started></date_started>
            <years></years>
            <position></position>
            <salary></salary>
            <e_increase></e_increase>
            <e_raise></e_raise>
            <photo></photo>
    </employee>
</employees>

我将要删除的员工的EmpID存储在名为$ ID的变量中。我想完全删除该员工。我尝试过这样的事情:

foreach ($doc->getElementsByTagName('employee') as $employee) {
    if($employee->getAttribute('EmpID') === $ID) {
        foreach ($employee as $node) {
    $node->parentNode->removeChild($node);
    }
    }
}

和其他循环试图让事情变得相似,就像在这些帖子中一样:PHP XML remove element and all children by nameRemove all children from a XML Node PHP DOM,但我无法让它成功运作。

任何帮助都将不胜感激,谢谢。

3 个答案:

答案 0 :(得分:1)

你去吧:

$xml = '<employees>
        <employee EmpID="1">
            <first_name></first_name>
            <last_name></last_name>
            <ssnum></ssnum>
            <status></status>
            <contact_info>
                    <office_phone></office_phone>
                    <email></email>
                    <cell_phone></cell_phone>
            </contact_info>
            <access_info level="user">
                    <username></username>
                    <password></password>
            </access_info>
            <department></department>
            <date_started></date_started>
            <years></years>
            <position></position>
            <salary></salary>
            <e_increase></e_increase>
            <e_raise></e_raise>
            <photo></photo>
    </employee>
     <employee EmpID="2">
            <first_name></first_name>
            <last_name></last_name>
            <ssnum></ssnum>
            <status></status>
            <contact_info>
                    <office_phone></office_phone>
                    <email></email>
                    <cell_phone></cell_phone>
            </contact_info>
            <access_info level="user">
                    <username></username>
                    <password></password>
            </access_info>
            <department></department>
            <date_started></date_started>
            <years></years>
            <position></position>
            <salary></salary>
            <e_increase></e_increase>
            <e_raise></e_raise>
            <photo></photo>
    </employee>
</employees>';

$doc = new DOMDocument();

$doc->loadXML($xml);

$selector = new DOMXPath($doc);
$els = $selector->query('/employees//employee');

//or
//$els = $doc->getElementsByTagName('employee');


foreach($els as $el){
    if($el->getAttribute('EmpID') == 1){
        $el->parentNode->removeChild($el);
    }
}

$xml = $doc->saveXML();

echo($xml);

答案 1 :(得分:1)

$ID = '';

$dom = new DOMDocument();
$dom->loadXML($xml_string);

$xpath = new DOMXpath($dom);
$nodes = $xpath->evaluate('/employees/employee[@EmpID = "' . $ID . '"]');
foreach ($nodes as $node) {
    $node->parentNode->removeChild($node);
}

答案 2 :(得分:1)

查看我的xml我有点不同..试一试。

foreach ($doc->getElementsByTagName('employee') as $employee) {
    if($employee->getAttribute('EmpID') === $ID) {
       $employee->parentNode->removeChild($employee);
       // $dom->save($xmlFile);
    }
}