开发者

I have problem loading all items from XML with for each

I'm low level as3 programmer and I need help whit this code:

I have gallery XML file:

 <gallery>
    <item>
        <id>1</id>
        <strana>0</strana>
        <naslov>Lokacije</naslov>
        <aktuelno>1</aktuelno>
        <slika>1.jpg<开发者_开发问答;/slika>
    </item>
    <item>
        <id>2</id>
        <strana>2</strana>
        <naslov>Coaching</naslov>
        <aktuelno>1</aktuelno>
        <slika>2.jpg</slika>
    </item>
    <item>
        <id>3</id>
        <strana>0</strana>
        <naslov><![CDATA[O.Å . Bratstvo - panel]]></naslov>
        <aktuelno>0</aktuelno>
        <slika>3.jpg</slika>
    </item>  
</gallery>

And:

var loader: URLLoader = new URLLoader(); loader.load(new URLRequest("gallery.xml");

var xml = new XML(evt.target.data);
for each(var item in xml..item) {
    centralniText.htmlText = item.slika;
}

only shows last item from XML file:

3.jpg

I want all. Please help.


After storing your loaded xml data in an XML object:

var xml:XML = XML(URLLoader(event.target).data); 

All you have to do is return an xml list value from the previous XML object's children() method:

var xmlList:XMLList = xml.children();

Then you can set it as your TextField object's htmlText property's value:

centralniText.htmlText = xmlList;

[UPDATE]

To access a specific element you can do the following:

trace(xmlList.slika[0]); // outputs: 1.jpg
trace(xmlList.slika[1]); // outputs: 2.jpg
trace(xmlList.slika[2]); // outputs: 3.jpg


In your for each loop, you always assign the current item’s value to the text field, overwriting previous values. So you need to append the text, for example like this:

var text:String = '';
for each(var item in xml..item) {
    text += item.slika;
}
centralniText.htmlText = text;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜