Results 1 to 8 of 8

Thread: [RESOLVED] DOMdocument - removing a node and its children

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [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?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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>

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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?

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: DOMdocument - removing a node and its children

    Damn, that's what I get for being distracted for half an hour.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: DOMdocument - removing a node and its children

    Quote Originally Posted by noahssite View Post
    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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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.

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: DOMdocument - removing a node and its children

    Quote Originally Posted by penagate View Post
    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
  •  



Click Here to Expand Forum to Full Width