How to delete a element by tag name with all elements inside it

后端 未结 2 910
盖世英雄少女心
盖世英雄少女心 2021-01-28 15:49

I want to use the dom removeChild function in php to remove everything between a tag.

my xml looks like


  text

        
相关标签:
2条回答
  • 2021-01-28 16:09
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $dom->getElementsByTagName('root')->item(0)
       ->removeChild($dom->getElementsByTagName('remove')->item(0));
    

    This is very specific, though. You can use XPath if you need more generality:

    foreach ($xpath->query('//remove') as $node) {
       $node->parentNode->removeChild($node);
    }
    
    0 讨论(0)
  • 2021-01-28 16:14

    You can use XPath and delete over XPath the node.

    Use DOM and XPath to remove a node from a sitemap file

    PHP SimpleXML - Remove xpath node

    here on Stackoverflow are a lot of posts. Perhaps you should search here at first.

    0 讨论(0)
提交回复
热议问题