2

i want to remove all children from my xml file before i fill it up again (or create an update but that seemed alot harder). So what i did is

    $file = "data.xml";

$xml=simplexml_load_file($file);

$teller=0;
foreach( $entries as $entry ) {
foreach ($xml->xpath('//concerts') as $desc) {
    if($teller == 0)
    {
    $lol=$desc->children();
    unset($lol);
    }
    $concert = $desc->addChild( 'concert' );
    $concert->addChild( 'artist', array_shift( $entry ) );  
    $concert->addChild( 'location', array_shift( $entry ) );    
    $concert->addChild( 'date', array_shift( $entry ) );
    $teller++;
}    
}


file_put_contents($file, $xml->asXML()); 

But this doesn't remove anything, any ideas on what i did wrong?

hakre
  • 193,403
  • 52
  • 435
  • 836
Toon Van Dooren
  • 613
  • 1
  • 12
  • 27
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Feb 26 '12 at 13:04
  • @Gordon not rly a duplicate, the adding works perfectly i just want to wipe all children of concerts before i start adding again :) – Toon Van Dooren Feb 26 '12 at 13:13
  • it shows create, read, update and delete (CRUD) - not just adding. – Gordon Feb 26 '12 at 13:17
  • @Gordon ye but i rly want a delete of all children of concerts... cant get it right... the layout is – Toon Van Dooren Feb 26 '12 at 14:11

3 Answers3

6

Here is one possible solution (online demo):

$xml = <<< XML
<?xml version='1.0' encoding='utf-8'?>
<concerts>
    <concert>
        <artist></artist>
        <date></date>
    </concert>
</concerts>
XML;

$concerts = simplexml_load_string($xml);

foreach ($concerts->xpath('/*/concert/*') as $child)
{
    unset($child[0]);
}

echo $concerts->asXML();

Marking this CW because how to delete elements is given in my supplied closevote and this answer only expands on this. And this now has been edited showing the self-reference method to delete a SimpleXML element node as outlined in an answer of the question "Remove a child with a specific attribute, in SimpleXML for PHP" which is also a possible duplicate.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • I just came up with a similar solution after i took my break xD if($teller == 0) { while($xml->concert) { unset($xml->concert); } – Toon Van Dooren Feb 26 '12 at 14:50
  • 1
    @Toon cw means community wiki, e.g. i dont get reputation from it. `unset($xml->concert)` will remove the entire concert node including children. my solution above only removes the children of any concert nodes. depends on what you want, but good if you figured it out now. – Gordon Feb 26 '12 at 14:55
1

By unsetting $lol, you have only unset a variable you created based on the children, not the children themselves.

Looking at the xPath query, you will likely want to unset($xml["concerts"]);

Also, I'm not sure if it's just because you gave a small piece of the code, but $entries is never defined, so you would never enter the loop.

conrad10781
  • 2,223
  • 19
  • 17
0

You can use XQuery to do the job:

let $concerts := <concerts>
    <concert>
        <artist></artist>
        <date></date>
    </concert>
</concerts>
return {
    delete node $concerts/concert/*;
    $concerts
}

You can try the example above live at http://www.zorba-xquery.com/html/demo#OLYylalXNV+ZVQbkRPVULLkd4Lo=

Instructions on how to install the XQuery PHP extension are available at http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery

wcandillon
  • 2,086
  • 19
  • 19