Monday, September 13, 2010

Working with XML data using AS3: Part 4

Today we will learn how to access XML-object's attribute values, as well as how to cycle through the values.

I am using the same code as I used in the previous tutorials, the only thing I am changing is the parseXML function. I have also created a new XML file for this code, because I am going to work with attributes. My XML file (catalog2.xml):

<catalog>

<product name="Chair" price="$50">
Something to sit on
</product>

<product name="Sofa" price="$100">
Something soft to sit on
</product>

<product name="Table" price="$70">
Something to put stuff on
</product>

</catalog>

As you can see, I have set products' names and prices as attributes for the nodes. Why don't I have description as attribute too? Because, for me it is easier that way. Names of the products usually consist of 1-3 words, price also doesn't take too much space, but description can sometimes be very long. It doesn't matter how do you form your XML file, but it is always a plus when you don't get lost in your own document and have everything sorted and readable.

Anyway, we need to access the attributes through AS3. They, just like normal values, are property-like (E4X standards), the only difference is that we need to put the @ symbol in front of attribute names:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
trace(xmlData.product.@name);
}

This will return:

XML file loaded!
Parsing:
-----------------
ChairSofaTable

An example, showing how to access individual parameters:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
trace(xmlData.product.@name[2]);
}

The magical output window says:

XML file loaded!
Parsing:
-----------------
Table

An alternative way to access attributes:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
trace(xmlData.product.attribute('name'));
}

We can use the for...each loop to get all the values (or attributes) from an XML object. If you don't know anything about this loop, you can read about it in this tutorial.

Example of accessing all the description values:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
for each (var desc in xmlData.product){
trace(desc);
}
}

Output window:

XML file loaded!
Parsing:
-----------------
Something to sit on
Something soft to sit on
Something to put stuff on

We can loop through attributes too. All you have to do is tweak the code a little bit:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
for each (var namez in xmlData.product.@name){
trace(namez);
}
}

And the results:

XML file loaded!
Parsing:
-----------------
Chair
Sofa
Table

There we go. Thanks for reading!

Related:

Working with XML data using AS3: Part 1
Working with XML data using AS3: Part 2
Working with XML data using AS3: Part 3
Working with XML data using AS3: Part 5
Working with XML data using AS3: Part 6
Working with XML data using AS3: Part 7
Working with XML data using AS3: Part 8
Working with XML data using AS3: Part 9
Working with XML data using AS3: Part 10

No comments:

Post a Comment