PDA

View Full Version : XML, external images and resizing



murphsy
October 16th, 2008, 11:48 AM
Hi there,

I've been puzzling with this for quite a bit. I went through a ton of xml+cs3 tutorials, and quite a few external images ones. I think I got them both nailed. The xml+cs3 tutorial on this website really shone a light on how this integration works.

However I want to have an xml that feeds a couple of nodes to an image loader. A horizontal image scroller to be exact.

So I know how to load an external image, through using an url and a movieclip. I know how to read a xml-file and how to filter it. But I can't get a grasp on how to read an xml-file and use the urls found there to load the images.

The xml file looks like this:

<gallery>

<foto id="1">
<loc>fotos/foto1.jpg</loc>
<thumb></thumb>
<desc></desc>
</foto>

<foto id="2">
<loc>fotos/foto2.jpg</loc>
<thumb></thumb>
<desc></desc>
</foto>

</gallery>

Loc being the location of the photo. (Thumb the thumbnail, desc the description - but this is for a later date)

How do I get my horizontal gallery to display foto 1, 2 and 3 next to eachother in boxes of 110x110px? It sounds so simple...

Thanks in advance

mathew.er
October 16th, 2008, 06:12 PM
just try something like this...

var data : XML = <gallery>

<foto id="1">
<loc>fotos/foto1.jpg</loc>
</foto>

<foto id="2">
<loc>fotos/foto2.jpg</loc>
</foto>

</gallery>;

var currentX : Number = 0;

for each ( var foto in data.foto ) {
var loc : String = foto.loc;
var l : Loader = new Loader ();
l.load ( new URLRequest ( loc ) );
l.x = currentX;

addChild ( l );

currentX += 110 + 10 // whatever gap you want there
}

murphsy
October 17th, 2008, 05:35 AM
Thanks a lot for the quick answer.

However, this part is what I still get to work. You now use inline xml (sort of) to load images. The problem I have however is with loading an external xml, one which is hosted on a website for example. :)

Could you explain how, instead of the inline xml, I would call upon the <loc>fotos/foto1.jpg</loc> which is written down in an external xml-file?

For example, I'm using the exact script you've written above, apart from the xmlbit. The thumbnail/photo location is aviable in an xml file named 'thisxmlrocks.xml' in a node called <thumbloc>.

I can get AS3 to work so I can actually trace the info in the xml, filter it for thumbnailloc's, etc, but if I try to couple/pair/link that with the code you suggested I fall flat on my face!

Thanks again for the reply, appreciate it, hope you can steer me a bit more :)

::edit:: removed a comment that could be misunderstood for ungratefulness or unfriendliness

mathew.er
October 29th, 2008, 07:52 AM
To work with an external XML file, you would need a loader and an event handler...

var l : URLLoader = new URLLoader ();
l.addEventListener ( Event.COMPLETE, xmlLoadCompleteHandler );
l.load ( new URLRequest ( 'yourData.xml' ) );

function xmlLoadCompleteHandler ( e : Event ) : void
{
var l : URLLoader = e.currentTarget as URLLoader;
var data : XML = new XML ( l.data );

// and continue with the previous script starting after the data variable declaration
}