<root>
   <vipfriends>
<friend>
<name>Mach</name>
<password>12345678</password>
<priviledge>vip</priviledge>
<sex>male</sex>
<tel>02552345332</tel>
<category>Business</category>
<view>myview</view>
<record>test</record>
<emails>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
</emails>
</friend>
<friend>
<name>Tom</name>
<password>42345678</password>
<priviledge>special</priviledge>
<sex>female</sex>
<tel>13076587654</tel>
<view>myview</view>
<category>Friend</category>
<record>tees</record>
<emails>
<email>[email protected]</email>
<email>[email protected]</email>
<email>[email protected]</email>
</emails>
</friend>
...
</vipfriends>
</root>如何能根据<name>找删除掉整个<friend>节点呢 比如我想查到name = Tom 的friend节点 然后删除friend ,请各位赐教

解决方案 »

  1.   

    参考PHP手册:
    http://www.php.net/manual/en/book.domxml.php
      

  2.   

    foeach($value as $node){
    return $node->getElementByTagName().fristChild.nodeValue;
    }
      

  3.   

    貌似用JQuery 很方便就能实现!!
      

  4.   

    我也为这个问题困扰过,常规方法是有问题的,因为你每删除一个节点会对其他变量产生影响(所以我估计是用类似指针的方法实现的)。我最后的方法是搜到的:http://zhidao.baidu.com/question/264025265.html
      

  5.   

    我自己修改后的代码:
    $rootNode = $xmlDoc->getElementsByTagName('vipfriends')->item(0);
    $nodes = $rootNode->getElementsByTagName('friend');
    $x = 0;
    $length = $nodes->length;
    //由于删除一个节点会影响数组长度,所以要特殊处理
    while($length > $x){
    $temp = $nodes->item($x);
    if(($temp->getElementsByTagName('name')->item(0)->nodeValue) == 'Tom'){
    $rootNode->removeChild($temp);
    $x--;
    $length--;
    }
    $x++;
    }