Ultra basic XML help please

I’ve never used XML before, at all.

However I have an upcoming project that’s going to have a big image gallery, that is likely to change often, so I can see XML would be very useful for speed of editing/adding etc.

I’m a complete XML noob then, and I’m just doing a little test with some text, and I can’t seem to access the Array outside the xml.onLoad function.

Could you tell me what I am doing wrong?

Here is the xml…

<?xml version="1.0" encoding="iso-8859-1"?>
<writing>
<para>
OK this is paragraph 1
</para>
<para>
And this is paragraph 2
</para>
<para>
Bloody hell, XML seems like a bit of a nause
</para>
</writing>

And here is the AS…

stop();
var xml:XML = new XML();
xml.ignoreWhite = true;
var description:Array = new Array();
xml.load("writing.xml");
xml.onLoad = function(loaded) {
 if (loaded) {
  paragraphs = this.firstChild.childNodes;
  for (var i = 0; i<paragraphs.length; i++) {
   description.push(paragraphs*.firstChild);
  }
 }
[COLOR=black]** desc.text = description[2];**[/COLOR]
};

Now in the above code, my dynamic text field named “desc” correctly displays “bloody hell, XML is a nause”.

However, when the line in bold is placed outside the onLoad function, it returns undefined, and when traced, the description array returns nothing at all.

I need to access the array outside of this function.

What am I doing wrong?

Cheers,
Joe

Let me tell you why its undefined, the array is empty until the XML file has been loaded. And if you place the “desc.text = description[2];” outside of the function, it gets called before the file has been loaded :slight_smile:

To get around this, you can either call a function from within the xml.onLoad() or put the “desc.text = description[2];” on the next frame.

Is that it? :smiley:

Thanks, I’ll give that a go. That crossed my mind, until I decided it wasn’t that, because I’m testing it locally obviously, so the XML file should be loaded immediately, AND I put the “desc.text = description[2];” after the code populating the array.

Well, I made a simple test, and thats the case :slight_smile: I tried putting it after the load call aswell, but the interval between actionscript calls and actually loading data from a file is too big.

Anyway, good luck now!

Superb, that was indeed it, I put it on the next frame and it worked absolutely fine, I also called from another function elsewhere, and it worked fine too. Thanks.

Incidentally, one more problem - apostrophes are not being displayed correctly.

“It’s” is being displayed as “it’s” :smiley:

Any ideas?

Cheers,
Joe

Nevermind, figured it out, I just had to stick on nodeValue after firstChild to register it as text.

I have to say, while XML is undoubtedly useful, and pretty easy too - somehow I find it annoying to work with.

Me too, I hate working with XML in flash, although as you say, its useful.