Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML document into Flash using ActionScript

Status
Not open for further replies.

Sarky78

Programmer
Oct 19, 2000
878
GB
Right then I have been given the task of reading an XML document into Flash using ActionScript. I have a whole 1 days experience with actionscript and not much more than that in Flash. in this time i ahve managed to get flash to download the file am now trying to loop through it.

this is what i have so far:

on (release) {
myXML = new XML();
myXML.load("arch_sites.xml");
myXML.ignoreWhite = true;
function myOnLoad () {
for (i=0; i<=myXML.childNodes.length; i++) {
_root.a = myXML.childNodes;
}
}
}

when i run this i either get nothing or get all of the xml document output into the textbox a.

this is the format of the xml file that is set in stone by the way:

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<Sites>
<Site>
<SiteName>a</SiteName>
<XCoord>3560</XCoord>
etc...
</Site>
<Site>
<SiteName>b</SiteName>
<XCoord>0653</XCoord>
etc...
</Site>
</Sites>

what i want to be able to do eventually is to loop through this xml document and only output certain elements, so loop over the whole document and grab some of the XMl elements for instance XCoord one time and then SiteName another time.

can anyone point me to a tutorial that would help me to do this, or give me a few pointers on how this would be achieved?

any help appreciated !
 
Pretty easy once you get your head around it... take these four lines

trace(xmlText.firstChild.childNodes[0].childNodes[0].childNodes);
trace(xmlText.firstChild.childNodes[1].childNodes[0].childNodes);
trace(xmlText.firstChild.childNodes[0].childNodes[1].childNodes);
trace(xmlText.firstChild.childNodes[1].childNodes[1].childNodes);

Which will output the values you have stored in the XML in this order:

a
b
3560
0653

firstChild takes you to <sites>..
its first childNode ([0])is <site>..
its first childNode ([0])is <sitename>..
its childnodes contain your value etc..

You can therefore use a 'for' or 'while' loop to set an index and loop through all of the childnodes and extract their values, and nest loops to really get through the XML tree.

Code:
for(i=0;i<totalNodes;i++){
trace(xmlText.firstChild.childNodes[0].childNodes[i].childNodes);
}
 
thats a heck of a lot easier than what i had written in the end. the only problem being is that when i run this script i get 'undefined' appear four times in the output window.

I am using the exact xml file from above (with the etc... removed of course) and this is the action script code:

on (release) {
myXML = new XML();
loaded = myXML.load(&quot;sites.xml&quot;);
function myOnLoad () {
trace(xmlText.firstChild.childNodes[0].childNodes[0].childNodes);
trace(xmlText.firstChild.childNodes[1].childNodes[0].childNodes);
trace(xmlText.firstChild.childNodes[0].childNodes[1].childNodes);
trace(xmlText.firstChild.childNodes[1].childNodes[1].childNodes);
}
myXML.onLoad = myOnLoad;
}

any idea what is going wrong here ?

thanks

Tony
 
oops wrong code...I am so not awake this morning...

on (release) {
myXML = new XML();
myXML.load(&quot;sites.xml&quot;);
function myOnLoad () {
trace(myXML.firstChild.childNodes[0].childNodes[0].childNodes);
trace(myXML.firstChild.childNodes[1].childNodes[0].childNodes);
trace(myXML.firstChild.childNodes[0].childNodes[1].childNodes);
trace(myXML.firstChild.childNodes[1].childNodes[1].childNodes);
}
myXML.onLoad = myOnLoad;
}
 
It's probably a whitespace issue which Flash will read as an empty node (ie 'undefined') - try this if you're working with MX (or a late release Flash 5 player):

myXML.ignoreWhite=true;

Which will strip out the whitespace, otherwise if you're working with an early release of the 5 player or are writing a 5 compatible application then you'll have to strip out the whitespace manually.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top