appendChild doesn't work correctly?
I've got an xml file:
<filmlist>
<user num="1">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
<user num="2">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
</filmlist>
At the start I read this file, count the number of 'user' tags and want to add a next 'user' tag to it, so it should look like this:
<filmlist>
<user num="1">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
<user num="2">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
<user num="3">
<film>clip02.flv</film>
<film>clip01.flv</film>
<film>clip01.flv</film>
</user>
</filmlist>
To do this I use:
var loadedXML:XML = new XML(event.target.data);
newUser = loadedXML.user.length() + 1;
var str:String = "<user num='" + (newUser ) + "'>\n";
//
for (var s:int=0; s<selectedMovies.length; s++)
{
str += "<film>" + selectedMovies
+ "</film>\n";}
//
str += "</user>";
loadedXML.appendChild(new XML(str));
//;
xmlToSend = new XML(loadedXML);
The xmlToSend object I send to my php script:
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$data = $GLOBALS["HTTP_RAW_POST_DATA"];
$file = fopen("filmpjes.xml", "w");
fwrite($file, $data);
fclose($file);
if (!$file) {
echo("<stuff>Server unable to create file.</stuff>");
} else {
echo("<stuff>File saved.</stuff>");
}
} ?>
The weird thing is, when I trace xmlToSend right before accessing my php script it traces correctly:
<filmlist>
<user num="1">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
<user num="2">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
<user num="3">
<film>clip02.flv</film>
<film>clip01.flv</film>
<film>clip01.flv</film>
</user>
</filmlist>
When the php script saves the new xml file however, appendChild doesn't seem to have worked ok. Instead of adding a next 'user' tag' it has replace the last one (2) with the new, so it looks like this.
<filmlist>
<user num="1">
<film>clip02.flv</film>
<film>clip01.flv</film>
</user>
<user num="2">
<film>clip02.flv</film>
<film>clip01.flv</film>
<film>clip01.flv</film>
</user>
</filmlist>
Have I done something wrong with appendChild? Which makes it look ok when I trace the xmlToSend XML object, but writes it with always the last node replaced by the new instead of adding a node???
