[RESOLVED] DOMdocument - removing a node and its children
Hello here is an outline of my XML file.
Code:
<userlist>
<user>
<name>Bob</name>
<time>11:04:23</time>
</user>
<user>
<name>John</name>
<time>11:02:03</time>
</user>
</userlist>
Here is the PHP file that adds to the XML file, if anyone needed to see it.
Code:
<?php
$dname = $_POST['dname'];
$ptime = $_POST['ptime'];
////////////////////////////////////
$doc = new DOMDocument();
$doc->load('userupdate.xml');
$user = $doc->createElement("user");
$doc->getElementsByTagName('userlist')->item(0)->appendChild($user);
$name = $doc->createElement("name");
$user->appendChild($name);
$time = $doc->createElement("time");
$user->appendChild($time);
$textName = $doc->createTextNode($dname);
$name->appendChild($textName);
$textTime = $doc->createTextNode($ptime);
$time->appendChild($textTime);
$doc->save("userupdate.xml");
?>
What I need to do is to loop through the XML file, using the DOMdocument, and remove all 'users' that have a time that is 2 seconds less than the current UTC time. Since each 'user' element has children they will have to be deleted too.
How do I do this?
Re: DOMdocument - removing a node and its children
this seems to accomplish just that:
PHP Code:
<xmp>
<?php
date_default_timezone_set("America/Denver");
$xml = new DOMDocument();
$xml->load("domdocument.xml");
$users = $xml->getElementsByTagName("user");
$queue = array();
//loop through each user
foreach($users as $user){
//get the time element for this user
$time = $user->getElementsByTagName("time")->item(0)->nodeValue;
//if the time is 2 seconds less than the current time, remove them
if(strtotime($time) < time() - 2){
//queue, because removing right away will cause problems with the loop!
$queue[] = $user;
}
}
//run through our queue of nodes to delete
foreach($queue as $node){
$xml->documentElement->removeChild($node);
}
echo $xml->saveXML();
?>
</xmp>
Re: DOMdocument - removing a node and its children
Thanks, but I read somewhere that using removeChild on a node that has children can result in error unless you delete the children first. Have you heard this before? Can it be confirmed?
Re: DOMdocument - removing a node and its children
PHP Code:
<?php
date_default_timezone_set('UTC');
$doc = new DOMDocument();
$doc->load('test.xml');
$xpath = new DOMXPath($doc);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions();
$searchTime = time() - 2;
$users = $xpath->query('user[php:functionString("strtotime", time) = '.$searchTime.']');
foreach ($users as $user)
$user->parentNode->removeChild($user);
?>
I took your sentence "a time that is 2 seconds less than the current UTC time" literally. Obviously you can change this.
Re: DOMdocument - removing a node and its children
Damn, that's what I get for being distracted for half an hour.
Re: DOMdocument - removing a node and its children
Quote:
Originally Posted by
noahssite
Thanks, but I read somewhere that using removeChild on a node that has children can result in error unless you delete the children first. Have you heard this before? Can it be confirmed?
Not true.
Re: DOMdocument - removing a node and its children
Thanks. I've never used DOMXPath before. I don't usually use XML, I just had to write a Flash Based Chat Room and chose to use XML instead of a database for flexibility in different environments.
EDIT: @penagate Thanks for clarifying.
Re: DOMdocument - removing a node and its children
Quote:
Originally Posted by
penagate
Damn, that's what I get for being distracted for half an hour.
I like yours better. I had never used DOMDocument and just thought it would be fun to mess around with it for a little ;)