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

array from XML

New Here ,
Oct 16, 2008 Oct 16, 2008

Copy link to clipboard

Copied

Hi,
This is probably a VERY basic question, but I have been struggling with it for a while.

I have a gallery that loads random images from an XML files. I am trying to get the XML data into an array, but I must be missing something.

Here is my XML:

quote:

<slideshow>
<slide data=" http://www.sandradussault.com/xml/flash-images/01.jpg" caption="image 01" />
<slide data=" http://www.sandradussault.com/xml/flash-images/02.jpg" caption="image 02." />
<slide data=" http://www.sandradussault.com/xml/flash-images/03.jpg" caption="image 03" />
<slide data=" http://www.sandradussault.com/xml/flash-images/04.jpg" caption="image 04" />
</slideshow>


And what I am using now to access it in AS3 is:

quote:

var xml:XML = new XML();
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("slideshow.xml"));
xmlLoader.addEventListener(
Event.COMPLETE,
function(evt:Event):void {
xml = XML(evt.target.data);
}
);

// Picture changing function
function changePicture(pict:Number):void {
pb.visible = true;
caption.text = xml.slide[pict].@caption;
loader.load(new URLRequest(xml.slide[pict].@data));
}


How do I change my code so it gets into an array?

I am trying this, but with no success:
quote:

var xmlArray:Array=[];
for each (var element:XML in xml.slide) {
array.push(element.toString());
}


Thank you
TOPICS
ActionScript

Views

742

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
LEGEND ,
Oct 16, 2008 Oct 16, 2008

Copy link to clipboard

Copied

You can talk to the xml as an array, and so wouldn't normally have to convert it. For example, this:

trace(xml.slide[1].@caption);

should show the caption of the second slide.

Here's a test you can do in a new movie's time line script:

var xml:XML = new XML(<slideshow>
<slide data=" http://www.sandradussault.com/xml/flash-images/01.jpg" caption="image 01" />
<slide data=" http://www.sandradussault.com/xml/flash-images/02.jpg" caption="image 02." />
<slide data=" http://www.sandradussault.com/xml/flash-images/03.jpg" caption="image 03" />
<slide data=" http://www.sandradussault.com/xml/flash-images/04.jpg" caption="image 04" />
</slideshow>);
trace(xml.slide[1].@caption);

If you want to break off some data, you can make an XMLList variable, which can be talked to as an array too. So this would work:

var slideinfo:XMLList = xml.slide;
trace(slideinfo[1].@caption);

One thing to be aware of, you check the length of an XMLList using length(), not length. Like:

trace(slideinfo.length());

would show 4.



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
New Here ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

Hi Colin,

Thank you so much for your reply.
It is working great when I the XMl is defined in the flash document. And I am able to get the random feature from it too. But if I am trying to get this info from an outside XML file, it gets me an error:

quote:

TypeError: Error #1010: A term is undefined and has no properties.
at flashBoard_slideshow_fla::MainTimeline/flashBoard_slideshow_fla::frame1()


I dont get it? I must be missing something. This is my code now:

quote:

var xml:XML = new XML();
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("slideshow.xml"));
xmlLoader.addEventListener(
Event.COMPLETE,
function(evt:Event):void {
xml = XML(evt.target.data);
}
);

var slideinfo:XMLList = xml.slide;
trace(slideinfo[1].@caption);

trace(slideinfo.length());

var i:Number = Math.round(Math.random()*slideinfo.length());

trace( );


Is it the XMLList that would need to be more defined? ahhh, this stuff can get so frustrating at some point..

Thanks

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
LEGEND ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

Doing inline functions might lead to issues, so instead of doing this:

var xml:XML = new XML();
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("slideshow.xml"));
xmlLoader.addEventListener(
Event.COMPLETE,
function(evt:Event):void {
xml = XML(evt.target.data);
}
);

try this:

var xml:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE,xmlloaded);
xmlLoader.load(new URLRequest("slideshow.xml"));

function xmlloaded(evt:Event):void {
xml = XML(evt.target.data);
}


Also, you can't start playing with the xml variable right away (until it's been filled in the complete function), so put your XMLList parts into the complete function, making it all be this:

var xml:XML;
var slideinfo:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE,xmlloaded);
xmlLoader.load(new URLRequest("slideshow.xml"));

function xmlloaded(evt:Event):void {
xml = XML(evt.target.data);
slideinfo = xml.slide;
trace(slideinfo[1].@caption);
trace(slideinfo.length());
}

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
New Here ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

Wow, Thank you Colin, you dont know how much you just helped me.. It works!! As much as I was hating actionscript 3.0 yesterday, today I find it pretty awesome :-)

So I got it like this:
quote:

var xml:XML;
var slideinfo:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE,xmlloaded);
xmlLoader.load(new URLRequest("slideshow.xml"));


function xmlloaded(evt:Event):void {
xml = XML(evt.target.data);
slideinfo = xml.slide;
trace(slideinfo[1].@caption);
trace(slideinfo.length());
var i:Number = Math.round(Math.random()*slideinfo.length());
loader.load(new URLRequest(xml.slide .@data));
caption.text = xml.slide
.@caption;
}



And now it loads a random image and its caption from the XML!

From there, do you think it will be possible to get it play like a slideshow, stop when the user press a button, then restart is there is no action for X minutes?

I am pretty sure it is related to the timer class. But will I have to include it in the function xmlLoaded? Or create another one?

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
Explorer ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

Also, when you declare variables, lots of the classes you will use require you to instantiate them with a 'new' keyword.

Example:

xml = new XML(evt.target.data);
slideInfo = new XMLList(xml.slide);

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
LEGEND ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

Right and wrong, I think. It is interesting that XML(sometext) works as well as new XML(sometext), and I agree that saying new XML(sometext) would be better. But with the XMLList I'm not so sure, because xml.slide is returning an XMLList, so new XMLList(xml.slide) would be converting an XMLList into an XMLList, which may not be needed.


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
Explorer ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

word

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
LEGEND ,
Oct 17, 2008 Oct 17, 2008

Copy link to clipboard

Copied

LATEST
Awesome efficiency in that reply!


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