Php 是否删除多个emty xml记录?

Php 是否删除多个emty xml记录?,php,xml,Php,Xml,我试图弄清楚,如果没有属性,如何删除多个xml“记录” 以下是我一直尝试到现在的方法: $xml = new DOMDocument(); $xml->preserveWhiteSpace = false; $xml->formatOutput = true; $xml->loadXML('<friends> <friend id="779"> <name>ML76</name> <games/>

我试图弄清楚,如果没有属性,如何删除多个xml“记录”

以下是我一直尝试到现在的方法:

$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML('<friends>
  <friend id="779">
    <name>ML76</name>
    <games/>
    <wins/>
  </friend>
  <friend id="131">
    <name>Puttepigen67</name>
    <games/>
    <wins/>
  </friend>
  <friend id="17">
    <name>rikkelolk</name>
    <games>3</games>
    <wins>2</wins>
  </friend>
  <friend id="">
    <name/>
    <games/>
    <wins/>
  </friend>
  <friend id="">
    <name/>
    <games/>
    <wins/>
  </friend>
  <friend id="">
    <name/>
    <games/>
    <wins/>
  </friend>
</friends>');

echo "<xmp>OLD \n". $xml->saveXML() ."</xmp>";

$opNodes = $xml->getElementsByTagName('friend');
$remove = array();

foreach ($opNodes as $node) {
    if ($node->attributes() == ""){
        $remove[] = $node;
    }
}

foreach ($remove as $node) {
    $node->parentNode->removeChild($node);
}

echo "<xmp>NEW \n". $xml->saveXML() ."</xmp>";
$xml=newDOMDocument();
$xml->preserveWhiteSpace=false;
$xml->formatOutput=true;
$xml->loadXML('
ML76
第67页
里克勒克
3.
2.
');
回显“OLD\n”$xml->saveXML();
$opNodes=$xml->getElementsByTagName('friend');
$remove=array();
foreach($opNodes作为$node){
如果($node->attributes()==“”){
$remove[]=$node;
}
}
foreach($remove as$node){
$node->parentNode->removeChild($node);
}
回显“新建\n”$xml->saveXML();
在最后一个XML->saveXML()中,我没有得到任何信息

我做错了什么


提前感谢:-)

使用
xpath

$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML('<friends>
  <friend id="779">
    <name>ML76</name>
    <games/>
    <wins/>
  </friend>
  <friend id="131">
    <name>Puttepigen67</name>
    <games/>
    <wins/>
  </friend>
  <friend id="17">
    <name>rikkelolk</name>
    <games>3</games>
    <wins>2</wins>
  </friend>
  <friend id="">
    <name/>
    <games/>
    <wins/>
  </friend>
  <friend id="">
    <name/>
    <games/>
    <wins/>
  </friend>
  <friend id="">
    <name/>
    <games/>
    <wins/>
  </friend>
</friends>');

$xpath = new DOMXPath($xml);

// prepare the xpath query to find the empty nodes
$node = $xpath->query("//friend[@id='']");

// if found, append the new "value" node
if( $node->length ) {
    foreach ($node as $n) {
        $n->parentNode->removeChild( $n );
    }
}
header('content-type: text/xml');
echo $xml->saveXML();
$xml=newDOMDocument();
$xml->preserveWhiteSpace=false;
$xml->formatOutput=true;
$xml->loadXML('
ML76
第67页
里克勒克
3.
2.
');
$xpath=newdomxpath($xml);
//准备xpath查询以查找空节点
$node=$xpath->query(“//friend[@id=''”);
//如果找到,则附加新的“值”节点
如果($node->length){
foreach($n节点){
$n->parentNode->removeChild($n);
}
}
标题('content-type:text/xml');
echo$xml->saveXML();

希望有帮助。

哦,是的。。。我没看到。谢谢:-)