0

我一直在这里和谷歌四处寻找并找到各种代码,但它们都不起作用。我假设这是因为它们都不符合我的确切情况。我有一个这样的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但我无法让它成功工作。

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

4

3 回答 3

1

查找我的 xml,我发现它略有不同.. 试试这个。

foreach ($doc->getElementsByTagName('employee') as $employee) {
    if($employee->getAttribute('EmpID') === $ID) {
       $employee->parentNode->removeChild($employee);
       // $dom->save($xmlFile);
    }
}
于 2013-04-22T17:50:35.370 回答
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);
于 2013-04-22T17:48:22.877 回答
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);
}
于 2013-04-22T17:48:29.343 回答