• Update/Delete info in XML document via PHP

    Miranda65M Member

    Update/Delete info in XML document via PHP

    This is what I got so far

    my .xml document

    
    
     Skuilnaam8Skuilnaam110cobus5
    

    and here i got my file which i want to run to delete my database entries and cleaning the xml entries but im have problem with my xml deletion part

    
    load( 'res/leaders.xml' );
    
    $masterElement = $retriever->documentElement;
    
    $masterContent = $masterElement->getElementsByTagName('users')->item(3);
    
    $oldContent = $masterElement->removeChild($masterContent );
    
    echo $retriever->saveXML();
    
    ?>
    

    I get the following error message:

    Catchable fatal error: Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in /srv/disk1/1296487/www/rekvasvra.uni.me/Gr10/Rente/cron.php on line 23
    

    can someone help me find a solution and edit the code for me please im a beginner and is strggling to solve the problem as far as I can see the problem must be

    $masterContent = $masterElement->getElementsByTagName('users')->item(3);
    
  • ShikhaTan Member

    What happens if you use:

    $masterContent = $masterElement->getElementsByTagName('users')->item(2);

    It maybe 0 indexed so 0,1,2 not 1,2,3. Not sure – haven’t used this myself.

  • Amit Member

    If we take a look at this line:

    $masterContent = $masterElement->getElementsByTagName('users')->item(3);
    

    You’re trying to fetch the third item of a list, a list which we just determined was empty. Thus it returns null, and it results in you trying to pass null to a method which expects an object.

    How to solve it involves some basic logic, which I am sure you can figure out on your own if you just think about it. There are many ways to test for what you want, after all.

  • ShikhaTan Member

    Just add:

    echo $retriever->save( 'res/leaders.xml' );
    

    after:

    echo $retriever->saveXML();
    

    and it will save back to the file.

  • SapnaVishwas Member

    I found the solution but there’s something else that is bugging me now

    load( 'res/leaders.xml' );
    
    $masterElement = $retriever->documentElement;
    
    $masterContent = $masterElement->getElementsByTagName('user')->item(0);
    
    $oldContent = $masterElement->removeChild($masterContent );
    
    echo $retriever->saveXML();
    echo $retriever->save( 'res/leaders.xml' );
    ?>
    

    This delete each record as I refresh it each time, isn’t there a way to delete all at once or create a table where I can tick off to delete each record? and after I deleted all the records isn’t there a way to tell me there are no more records and link me back to the index? in place of getting the error

Viewing 4 reply threads
  • You must be logged in to reply to this topic.