Jump to content

Recommended Posts

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

You're a brave man approaching DOMdocument. I've seen it chew and spit out many developers in the past. You'll have to excuse me, but I've limited experience with DOMDocument I've avoided it like the plague and used SimpleXML in the past :)

 

 

$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){
        $oldxml = $tempxml->removeChild($key);
     }
  }
}

 

Try the above to see if that makes any difference. My concern is you have a few objects there that are independent of one another. So we'll refine down until the issue becomes clear.

exeTrix, thanks for the reply. I like your algorithm MUCH better. Nice And Clean! Unfortunately, I get the same result. I like simpleXML and use it to output xml to the browser window, however, when I need to manipulate the xml, I'm forced in to domdocument. As you already know, domdocument isn't well documented... or widely used.

 

Here is potentially a clue to the problem, preservewhitespace=false is only removing white space when deleting parent nodes (blog) at the beginning or at the end of the XML. But never when the nodes are inbetween the first or last parent. And to your point, it seems like the there are two sets of objects being manipulated when only $tempxml should be the one processed.

 

Final comment, I've tried hard coding $pid and also assigning it from form input, I'm pretty sure $pid is not the issue. I think this may be a bug with the DOMDocument class. Thanks again for the assist!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.