Home » ActionScript 3.0, Flash CS4, XML

Looping through each XMLNode and each attributes within Node:

11 March 2010 No Comment
Share on Facebook

In this tutorial we are going to iterate through node in a XML. Intead of using direct approach of getting the XML Length(); We are going to use indirect approach to loop through the XML.

Here we are going to use For each statement and iterate through the nodes to access information intead of using index values.

Here is XMLManager class to load XML without pain.

Before proceeding we must know the difference between XML and XMLList.

XML:
XML is a single or collection of XMLNodes with in a rootnode.

XML example:


	
	

Note:
Here the root node is Library which contains Book Nodes.

XMLList:
A collection of XMLNode without root node.

XMLList example:

	
	

Next we will see how to interate through XMLNodes.

var _xml:XML = 
var _xmlList:XMLList = _xml.Book;

for each(var node in _xmlList)
trace(node.attribute('id'))

There may be situation that the number of attributes with in a node may not be know. So we should loop through the all the attributes of a XMLNode.

var _xml:XML = 
var _xmlList:XMLList = _xml.Book;
trace("Attribute::Value");
trace("----------------")
for each(var node in _xmlList){
		for each(var attribute in node.attributes())
		trace(attribute.name()+"::"+attribute)
		trace("********Node End*********")
	}

Output:

Attribute::Value
—————-
id::1
name::Book1
author::XXXXXX
category::YYYYYY
********Node End*********
id::2
name::Book2
author::AAAAAA
category::ZZZZZZ
********Node End*********

Cheers :p

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner