Sunday, September 12, 2010

Working with XML data using AS3: Part 3

In today's tutorial we will learn how to access XML-object's data and extract different nod and tag values.

First of all, a little bit about ECMAScript for XML, known as E4X. E4X is a standard for making work with XML more simple with languages like Actionscript and Javascript. This method is a lot more simple to use if compared to DOM (Document Object Model) method that was used before. The E4X standard is similar in both Actionscript 3 and Javascript.

Why do we need to know this? Because now, thanks to E4X, we are able to retrieve data from the XML file easily referring to values the same way you refer to properties of an object.

However, to start using these features, we need to create a separate function (assuming we have the code from the previous tutorial) to parse the information in the XML object, just to make it moer simple. We will need to modify our readXML function first. Instead of tracing all the data, call a custom function called parseXML (which we will create later):

function readXML(event:Event):void
{
_data = new XML(event.target.data);
trace("XML file loaded!");
parseXML(_data);
}

Now for the most fun part, the parseXML function. This is where E4X kicks in. As I said before, we can refer to nods as properties of the object (the XML document). This way, we can return all the product nods (and all the nods inside):

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

Output:

XML file loaded!
Parsing:
-----------------
<product>
  <name>Chair</name>
  <description>Something to sit on</description>
  <price>$50</price>
</product>
<product>
  <name>Sofa</name>
  <description>Something soft to sit on</description>
  <price>$100</price>
</product>
<product>
  <name>Table</name>
  <description>Something to put stuff on</description>
  <price>$70</price>
</product>

We can get all the name values too:

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

Output:

XML file loaded!
Parsing:
-----------------
<name>Chair</name>
<name>Sofa</name>
<name>Table</name>

But what if we don't know where are the name tags located? We can search the whole document for them:

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

We know that we have multiple name tags. All of them together make an Array (it is not an actual Array, but it is very similar). This means that we can easily check any individual name tag, for example, the second one:

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

Output:

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

We can check how many name tags are there in total:

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

An important note: length() here is not a property, but a method. As I said before, this isn't an actual Array, but it works like one.

We are also able to get the values of attributes... find out how in the next tutorial!

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 4
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

4 comments:

Anonymous said...

It,s ok everything, but what about if we want to return something?
What if we want to give to an outside variable a name or a value of xmlData properties?
something like this

var myValue:Number = new Number()
function parseXML(xmlData:XML):void
{
trace(xmlData.product.name[1]);
myValue = xmlData.product.name[1]
}
trace(myValue)

how exact we should make this returning procedure?

Kirill Poletaev said...

You've done it the right way - you can use that code and it will work correctly.

Anonymous said...

well...it doesn't !
i have no other comment
it just does not...Try it !!
THAT is why i ask....
The fact is NOBODY knows ALL OVER THE INTERNET how to do this....

Anonymous said...

Everyone make ALL the assignements(menus, navbars, gallery, etc, etc....) INSIDE THE FUNCTION...but no one knows how exactcly return a value and use it outside

Post a Comment