Skip to main content
Inspiring
May 15, 2013
Answered

appendChild doesn't work correctly?

  • May 15, 2013
  • 1 reply
  • 1446 views

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

This topic has been closed for replies.
Correct answer kglad

there's no problem with your as code.  it will do what you want.

use different code to add something new and unique to user num '3' to make sure you're checking the correct server file and not looking at some cache'd file.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
May 16, 2013

there's no problem with your as code.  it will do what you want.

use different code to add something new and unique to user num '3' to make sure you're checking the correct server file and not looking at some cache'd file.

jiggy1965Author
Inspiring
May 16, 2013

Funny thing is that offline, with MAMP all works fine. But when I put it on my server somehow it doesn't work. Like it didn't got the time to save the updated xml file. I also encountered a few times that the xml after save was a version a few saves back. Like it saved a cached version of an xml file from somewhere. I'm using 'stop()' and only let it continue after the Event.COMPLETE when saving. So that the php file is online and doesn't have enough time to save shouldn't be a problem. In Safari I clear the cache of my online domain every time I upload a new swf version. It doesn't look for a cached xml file when I let it do a new loading of that xml file does it?

jiggy1965Author
Inspiring
May 16, 2013

Found this on actionscript.org about similar cache problems with xml files:

Put some random number after URL. Example:

var request:URLRequest = new URLRequest("http://www.site.com/page.html?rand="+Math.random()*1000);

And now my online version works too Is this a bug regarding cache and URLRequest? Anyone heard of this?