简体   繁体   中英

xml remove child node in php

I'm trying to remove the "druzenje" element by the 'id' attribute. I know that for that to happen, i have to remove all the child nodes from that element.

<?xml version="1.0" encoding="utf-8"?>
<druzenja>
    <druzenje id="1">
        <podaci attribute="some-data"/>
    </druzenje>
    <druzenje id="3">
        <podaci attribute="some-data"/>
    </druzenje>
</druzenja>

The code to remove is this...

    $druzenja = $this->dom->getElementsByTagName('druzenje');

    foreach($druzenja as $node) {
        if($node->getAttribute('id') == $id) {
            $podaciNodeList = $node->getElementsByTagName('podaci');
            foreach($podaciNodeList AS $podaciNode) {
                $node->removeChild($podaciNode);
            }

            $this->dom->documentElement->removeChild($node);
            return true;
        }
    }

I presume that removing a node by an 'id' attribute could be bad design but I based a lot of code on that and so far, there hasn't been any problems. I also have a code that removes the "podaci" element that works in a similar way and it does that with no problems, but here it fails. The code won't remove the "podaci" and "druzenje" element even though removeChild() returns the DomElement object which means its successful but it's not.

Any ideas?

$element = $this->dom->getElementById($id);
$element->parentNode->removeChild($element);

Someone kill me. I haven't been saving. I apologize for wasting someone's precious time.

getElementsByTagName() returns "live" result that you remove the elements from the DOM. Copy it to an array to get a fixed list.

$druzenja = iterator_to_array(
  $this->dom->getElementsByTagName('druzenje')
);

foreach ($druzenja as $node) {
  if ($node->getAttribute('id') == $id) {
    $node->parentNode->removeChild($node);
  }
}

Xpath would allow you to use more complex conditions. The result is not live.

$xpath = new DOMXpath($this->dom);
foreach ($xpath->evaluate('//druzenje[@id="'.$id.'"]') as $node) {
  $node->parentNode->removeChild($node);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM