|
-
Apr 13th, 2010, 06:14 PM
#1
Thread Starter
Frenzied Member
[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?
-
Apr 13th, 2010, 07:53 PM
#2
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>
-
Apr 13th, 2010, 08:30 PM
#3
Thread Starter
Frenzied Member
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?
-
Apr 13th, 2010, 08:37 PM
#4
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.
Last edited by penagate; Apr 13th, 2010 at 08:43 PM.
-
Apr 13th, 2010, 08:38 PM
#5
Re: DOMdocument - removing a node and its children
Damn, that's what I get for being distracted for half an hour.
-
Apr 13th, 2010, 08:39 PM
#6
Re: DOMdocument - removing a node and its children
 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.
-
Apr 13th, 2010, 08:50 PM
#7
Thread Starter
Frenzied Member
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.
-
Apr 13th, 2010, 10:27 PM
#8
Re: DOMdocument - removing a node and its children
 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 ;)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|