• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

appendChild doesn't work correctly?

Contributor ,
May 15, 2013 May 15, 2013

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 15, 2013 May 15, 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.

Votes

Translate

Translate
Community Expert ,
May 15, 2013 May 15, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 15, 2013 May 15, 2013

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 15, 2013 May 15, 2013

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2013 May 16, 2013

Copy link to clipboard

Copied

that's not a bug.

it's normal behavior of your browser and the reason i stated:  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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 16, 2013 May 16, 2013

Copy link to clipboard

Copied

Yes, thanks. Your remard about cache did make me look into that. And found people having similar problems where the old xml file was being used instead of the just updated one. Still weird that you would have to use this trick to fool the browser (which took me a while to find, they don't mention that in the IT books). Would expect for a 'new' URLRequest to make it load the same file again from the server as it is a new request. First clearing the 'request' variable like 'var request:URLRequest = null' and then running it again without the random part wouldn't work either I suppose? But he, it works and I've made a note of it for next time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2013 May 16, 2013

Copy link to clipboard

Copied

LATEST

the problem is unrelated to actionscript/flash. 

it's a browser issue just like if you visit a page that you've previously visited.  unless something special is done in the coding of that page, the browser will retrieve the page from its cache instead of re-downloading the page.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines