This
is a tutorial using FLASHMX. It pulls data from an xml document
named bookmarks.xml. The sample xml file is contained within the
ActionScript window. You will also notice that the comments give
a clear indication of what the code means. This is straight from
the FLASHMX code window. If you have any questions, e-mail
me.
/*
Programmer: JamesTadeo
Description: Bring in an external .xml file and
display node information by traversing parent and child
nodes in the bookmarks.xml document.
Terms
You Need to Learn and Remember:
firstChild
nodeName
nodevalue
nextSibling
attributes
Here's
the .xml document:
<?xml
version="1.0" ?>
<!--My BookMarks XML document -->
<bookmarks>
<bookmark
id="1">
<Description>James's Web sites</Description>
<URL>http://www.jamestadeo.com</URL>
</bookmark>
<bookmark
id="2">
<Description>IDEV site</Description>
<URL>http://www.idevcentral.com</URL>
</bookmark>
<bookmark
id="3">
<Description>Gala Bridal</Description>
<URL>http://www.galabridal.com</URL>
</bookmark>
<bookmark
id="4">
<Description>DoubleFlip</Description>
<URL>http://www.doubleflip.com</URL>
</bookmark>
<bookmark
id="5">
<Description>TSI Group</Description>
<URL>http://www.tsigroup.com</URL>
</bookmark>
<bookmark
id="6">
<Description>Brampton HomeSchoolers</Description>
<URL>http://spidey.mknet.com/learn.htm</URL>
</bookmark>
<bookmark
id="7">
<Description>Vintners</Description>
<URL>http://www.vinetowine.com</URL>
</bookmark>
</bookmarks>
*/
/*
The first thing you have to do to work with a .xml file is to create
an xml
object in FLASHMX. To do that, type this in:
*/
myXML=new
xml();
/*
You also want to make sure it ignores whitespaces. Whitespaces
in your xml document can cause problems.
*/
myXML.ignoreWhite=true;
/*
Now that you've made the xml object, you now have something to load
the .xml file's
contents in. So, the next step is to load your .xml file. In our
example, the file
name is bookmarks.xml. Before you do this, just make sure your bookmarks.xml
file is
in the same directory. It keeps things simple.
*/
myXML.load("bookmarks.xml");
/*
At this point, you want to make sure that the file is loaded
before you start working with it. Because if it's not ready and
you start pulling data from it, an error will occur. So you
ask and check if it's loaded.
*/
myXML.onLoad=function() {
//this will spit out "loaded" in the output window
//trace("Bookmarks.xml is loaded");
//this will spit out the entire .xml document in the
//output window
//gives you the entire xml document
//trace(myXML.firstChild);
//trace(myXML.childNodes);
//this gives you
//<bookmark id="1"><Description>James's
Web sites</Description><URL>http://www.jamestadeo.com</URL></bookmark>
//trace(myXML.firstChild.firstChild);
//this gives you:
//<Description>James's Web sites</Description>
//trace(myXML.firstChild.firstChild.firstChild);
//this gives you:
//James's Web sites
//trace(myXML.firstChild.firstChild.firstChild.firstChild);
//this gives you:
//James's Web sites
//trace(myXML.firstChild.firstChild.firstChild.firstChild.nodeValue);
//this gives you:
//<URL>http://www.jamestadeo.com</URL>
//trace(myXML.firstChild.firstChild.firstChild.nextSibling);
//this gives you:
//http://www.jamestadeo.com
//trace(myXML.firstChild.firstChild.firstChild.nextSibling.firstChild);
//this gives you:
//1
//trace(myXML.firstChild.firstChild.attributes.id);
//declare bookmarks to contain the entire contents of bookmarks.xml
bookmarks=myXML.firstChild;
//trace(bookmarks);
//<bookmark id="1"><Description>James's
Web sites</Description><URL>http://www.jamestadeo.com</URL></bookmark>
bookmark=bookmarks.firstChild;
//trace(bookmark);
bookmarksID=bookmarks.firstChild.attributes.id;
//trace(bookmarksID);
//<Description>James's Web sites</Description>
Description=bookmark.firstChild;
//trace(Description);
//James's Web sites
DescriptionValue=Description.firstChild.nodeValue;
//trace(DescriptionValue);
//<URL>http://www.jamestadeo.com</URL>
URL=Description.nextSibling;
//trace(URL);
//http://www.jamestadeo.com
URLValue=URL.firstChild.nodeValue;
//trace(URLValue);
//--------------ITERATION BEGINS----------------------
//trace("The number of childNodes is: " + myXML.firstChild.childNodes.length);
for(var i=0;i<myXML.firstChild.childNodes.length;i++) {
trace("The current value of i= " + i);
//declare bookmarks to contain the entire contents of bookmarks.xml
bookmarks=myXML.firstChild;
//trace(bookmarks);
//<bookmark id="1"><Description>James's
Web sites</Description><URL>http://www.jamestadeo.com</URL></bookmark>
//for this iteration, we need to know, which childNode we are working
with
bookmark=bookmarks.childNodes[i];
//trace(bookmark);
//bookmarksID=bookmarks.firstChild.attributes.id;
//trace(bookmarksID);
//gives you the id attribute (in our example it's incremental)
//but what could happen is that a node could be taken out and
//it will show the value of the attribute
bookmarksID=bookmarks.childNodes[i].attributes.id;
trace(bookmarksID);
//<Description>James's Web sites</Description>
Description=bookmark.firstChild;
//trace(Description);
//James's Web sites
DescriptionValue=Description.firstChild.nodeValue;
//trace(DescriptionValue);
//<URL>http://www.jamestadeo.com</URL>
URL=Description.nextSibling;
//trace(URL);
//http://www.jamestadeo.com
URLValue=URL.firstChild.nodeValue;
trace(URLValue);
}//for loop
} |