Thursday, December 6, 2012

Working with XML, XMLList, XMLListCollection in as3

I faced a trouble when i work with XML. It's teach me a lot. From that gathered info, i shared my thoughts.

First of all, we have to understand the XML and XMLlist. XML is well-formed structure of information. And we created the XML using XML class in as3.

XMLList is the collection of XML objects. We can collect the XML values use descent operator.

The main difference is XML has a single root and XMLList have multiple roots because it's a collection of XML objects.

For an Example:

var xml:XML = new XML(
                     <menu>
                        <item id="">
                            <screen>
                                <screen1>Value 1  </screen1>           
                                <screen2>Value 2 </screen2>
                            </screen>
                            <screen>
                                <screen3>Value 3 </screen3>                              
                            </screen>
                        </item>
                    </menu>);

You can traverse xml using descent operator(.) and Screen value in array-collection.

var xmlList:XMLList = xml.item.screen;
var valusAr:ArrayCollection = new ArrayCollection();
var tagnamesAr:ArrayCollection = new ArrayCollection();
for each(var item:XML in xmlList.children()){
            valusAr.addItem(item.text());
            tagnamesAr.addItem(item.localName());
}


The result will be
valuesAr :Value 1, Value 2, Value 3
tagnamesAr: screen1, screen2, screen3

Finally, XMLListCollection is nothing like arrayCollection class because the two class extends ListCollectionView class.

Using this we may use sort, filter properties for XMLList collection object.

No comments:

Post a Comment