0

domdocument 在任何地方(包括 SO)都没有很好的记录,所以我想我会在这里问这个问题。我有一个 PHP/XML 博客,并希望让博主能够一次删除多个博客文章。以下代码在两个帖子 ID 无序或不相互跟随时有效。

example - when post id 1 and post id 4 are selected, they both delete. 但是当post 1和post 2被选中时,只有2被删除。每次的行为都是一致的。有没有人遇到过这种情况,或者在我的代码中看到会导致这种情况的东西?

伪 XML:
<root>
<blog id=1><copy>Some copy</copy></blog>
<blog id=2><copy>Another block of copy</copy></blog>
<blog id=3><copy>Last chance copy</copy></blog>
</root>

删除博客文章的 PHP 代码(较大类的片段):

     /**
     * method usage: $objVar->deletePost($param)
     * delete client selected xml[blog] entries
     * @param array, $params : assign $_POST array as argument
     * @return string, XML
     */
    public function deletePost($params) {

    foreach ($params as $value){ // assign $param['delete[n]'] to array
        $pid=$value;   // array[n][id]
    }

    $xml = new DOMDocument(); // instance of current xml
    $xml->load($this->fp);    // path to file
    $xml->preserveWhiteSpace = false;

    //define xpath object, called with query() line 273
    $xpath = new DOMXpath($xml);

    //create temporary DOM xml
    $tempxml = $xml->documentElement;

    $pnode = $xml->getElementsByTagName('blog');
     foreach($pnode as $key){       //outter loop thru parent nodes
      foreach($pid as $v){          //inner loop thru pid array
      if ($key->getAttribute('id')==$v){    //if pid matches blog[id=n]
        $path = "//root/blog[@id=".$v."]";
        $nodelist = $xpath->query($path);
        $removenode = $nodelist->item(0);

        //remove the parent/children for matching ids
        $oldxml = $tempxml->removeChild($removenode);
      }
     }
    }
    echo $xml->saveXML();

  }

一如既往,提前感谢

4

0 回答 0