$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);
}
Deleting an xml node and all children via php
-
11-04-2022 - |
Вопрос
I have been looking around here and google and found various code, but none of them work. I'm assuming it's because none of them match my exact situation. I have an xml structure like this:
<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>
I have the EmpID of the employee I want to delete stored in a variable called $ID. I want to remove that employee completely. I have tried things like:
foreach ($doc->getElementsByTagName('employee') as $employee) {
if($employee->getAttribute('EmpID') === $ID) {
foreach ($employee as $node) {
$node->parentNode->removeChild($node);
}
}
}
and other loops trying to get things similarly, as in these posts: PHP XML remove element and all children by name and Remove all children from a XML Node PHP DOM but I have not been able to get this to work successfully.
Any help would be appreciated, thanks.
Решение
Другие советы
there you go:
$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);
Looking up my xml I have it slightly different .. Give this a try.
foreach ($doc->getElementsByTagName('employee') as $employee) {
if($employee->getAttribute('EmpID') === $ID) {
$employee->parentNode->removeChild($employee);
// $dom->save($xmlFile);
}
}
Не связан с StackOverflow