Search the Community
Showing results for tags 'php xml domdocument'.
-
Hi first time poster. I have a PHP/XML blog app and want to give users the ability to delete multiple blog post, with one form submit. The solution is working when blog post ids are not in order. That is if post id selected was 2 and 4, both nodes are deleted from the xml tree. But if the post id selected were 1 and 2, only 2 would be deleted. Has anybody else come across this behavior? Here's some code: psuedo xml <root> <blog id=1><copy>Some copy here</copy></blog> <blog id=2><copy>More copy</copy></blog> <blog id=3><copy>Even more copy</copy></blog> <blog id=4><copy>Last copy</copy></blog> </root> PHP, this is a method in a larger class /** * 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 multi-dim $param[delete][n] to two-dim array $pid=$value; // $value looks like array([0]=>[n],[1]=>[n]) } $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(); } Thanks in advance, rwhite35