by
kirupa | 17 July 2007
In the previous page,
we started learning how to access the data stored in an XML file. We began by
learning how to access the data directly, but that approach works only when you
have an idea of the XML file and the node names you will be using. In cases
where your XML data's structure is ambiguous, the indirect approaches
listed in this page will be helpful.
The indirect approach involves using loops and iterating
through the child nodes and extracting the information as
needed. When we traced the author information, we received
all of the author names in one single line. Our goal is to
figure out a way to iterate through our list and display the
information individually without directly accessing the
information using manually provided index values.
To do that, you should note that collections of XML information are always returned
as an XMLList, and an XMLList contains only XML objects. For example, the list
of authors you returned in the previous page were returned in the form of an
XMLList, and an XMLList supports many of the standard List operations beyond
supporting standard XML operations.
The following is the code I use inside our ParseBooks function for displaying
each author information using a loop:
- function ParseBooks(bookInput:XML):void
{
- trace("XML
Output");
- trace("------------------------");
-
- var authorList:XMLList
= bookInput.Book.author;
-
- for each
(var
authorElement:XML
in authorList)
{
- trace(authorElement);
- }
- }
Let's look at our above code in greater
detail:
- var authorList:XMLList
= bookInput.Book.author;
I first create an object called
authorList
that is of type XMLList, and I initialize authorList to the XMLList returned by
bookInput.Book.author.
- for each
(var
authorElement:XML
in authorList)
{
- trace(authorElement);
- }
In the above lines, I iterate through our authorList using a for-each
statement. Because this is a for-each statement, I do not worry about index
positions. Instead, I just specify the name of the current object and the
collection from which the objects are taken from. The rest is taken care of
behind-the-scenes.
Notice that your authorElement value is of type XML.
The reason authorElement is of type XML is because, like I mentioned before, an
XMLList (which authorList is) contains XML objects.
If you want to use a for loop instead of the
above for-each loop, the code would be:
- for (var
i:int
= 0;
i <
authorList.length();
i++)
- {
- var
authorElement:XML
= authorList[i];
- trace(authorElement);
- }
The only thing to note is that the loop ends when our index variable is less than the total number of items in authorList.
We get the total number of items by calling our XMLList object authorList's
length() method.
If you happen to not even know the name of of the nodes you are looking for,
you can use the more generic children()
function. The children function returns all of a node's children, and they are
returned in the form of an XMLList. Once you have your XMLList, you can process
the data in any way you choose:
For example, the following is an example on how to access information by
looping through a node's children:
- function ParseBooks(bookInput:XML):void
{
- trace("XML
Output");
- trace("------------------------");
-
- var bookChildren:XMLList
= bookInput.Book.children();
-
- for each
(var
bookInfo:XML
in bookChildren)
{
- trace(bookInfo);
- }
- }
Unlike in the previous examples where only the author information was
printed, this time all of our Book node's children - title and author - are
displayed:
You can filter by author checking your bookInfo XML object's
name property. The name property returns the
name of the node your data is stored in. For example, altering our above trace
code from trace(bookInfo) to
trace(bookInfo.name()) produces the following
output:
What you see is the node names of the data you saw earlier. With this
information, you can more selectively pick which information you want. For example, the following would be
the altered ParseBooks function for displaying only the authors generated by
using the children()
property:
- function ParseBooks(bookInput:XML):void
{
- trace("XML
Output");
- trace("------------------------");
-
- var bookChildren:XMLList
= bookInput.Book.children();
-
- for each
(var
bookInfo:XML
in bookChildren)
{
- if (bookInfo.name()
== "author")
{
- trace(bookInfo);
- }
- }
- }
Notice that I check to see what our current XML node's name is. If the node's
name matches what I am looking for (author), I then display the relevant
information.
You are done directly dealing with nested information
stored in nodes...for now. Starting on the
next page,
you'll learn how to retrieve attribute information.
Onwards to the
next page!
|