tags:

views:

40

answers:

2

I'm trying to edit some xml data. After this I want to save the data to file.

The problem is that the edited data isn't saved by simplexml but the node has changed.

$spieler = $xml->xpath("/planer/spieltag[@datum='" .$_GET['date']. "']/spielerliste/spieler");

for ( $i = 1; $i < 13; $i++ ){
    if (!empty($_POST['spieler' .$i ])){
        $spieler[$i-1] = $_POST['spieler' .$i];
    }
}
var_dump($spieler);
$xml->asXML("data.xml");

var_dump() shows the new data, but asXML() doesn't.

A: 

Make sure your script has write permission to data.xml

codaddict
Thats not the problem because adding some nodes works fine.
adnek
You are adding the nodes to the in-memory string.
codaddict
A: 

The XPath result array elements aren't PHP ($ref = &$var) references to the actual tree nodes, so this line

    $spieler[$i-1] = $_POST['spieler' .$i];

isn't modifying anything in the tree, you're simply overwriting an entry in a completely independent array.

Marc B
Ah ok thanks for this. So how can I change the tree? I know it is possible with '$xml->element[0] = "foo"' for example, but this isn't quite beautiful because i need to select the node with an attributes value.
adnek
You might be better off with the full DOM instead, and use `::replaceChild()` with the updated data. Haven't used SimpleXML myself, but there must be something in the returned xpath results to figure out where in the tree the result comes from.
Marc B