# XML in ActionScript

**URL:** <https://forum.kirupa.com/t/xml-in-actionscript/209659>\
**Category:** flash\
**Created:** [December 11, 2006, 10:17am UTC](https://forum.kirupa.com/t/xml-in-actionscript/209659 "2006-12-11T10:17:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![korkor5](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@korkor5](https://forum.kirupa.com/u/korkor5)\
**Post date:** [December 11, 2006, 10:17am UTC](https://forum.kirupa.com/t/xml-in-actionscript/209659/1 "2006-12-11T10:17:21Z")

</div>

Hi all,

I am creating a crawler in actionscript and i am taking an object oriented approach, which means that i have a class which is saved as an .as file. I then call the class in a .fla file.

The problem i am facing is that i am trying to read from an external xml file in the init function and populate an array with the contents of the xml file. I then try to print the length of the array but it prints undefined.

Actually i create a function that loads the xml file and returns an xml file. I then call that function in the init function. What i did was to create an xml object and assign it to the function like so: xmlfil = view(“filename”); but it gives me undefined when i run. Please help me out.

ActionScript class

dynamic class Crawler  
{

public var filename:String;

function Crawler(filename:String)  
{  
this.filename = filename;

```
}

```

function init()  
{

```
var xmlfil:XML = new XML("filename");

xmlfil = view("filename");

createTextField("scroller_txt", 1, -500,100, 2000, 50);

scroller_txt.html = true;
scroller_txt.background = true;
scroller_txt.backgroundColor = 0xFDF5E6;
scroller_txt._y = 300;
trace(scroller_txt.backgroundColor);
           
            
          
    var myArray:Array = xmlfil.firstChild.childNodes;
    trace(xmlfil);
    trace("the length of the array is"+ myArray.length);

for(var i=0; i&lt;myArray.length;i++)
{ 

            
    if(i%2 == 1)
{
   
    
    var sped = 10;

```

scroller\_txt.htmlText =" “+”\<img src=“E:\HELENA E stuff\Helena’s Game 10-23-2006\flash\ igo.jpg”\>" + myArray[1].firstChild.nodeValue;

for( m=0; m\<50; m++)  
{

```
if(m%2 == 1)
{

```

scroller\_txt.htmlText+= “\<img src=“E:\HELENA E stuff\Helena’s Game 10-23-2006\flash\ igo.jpg”\>”+ myArray[m+2].firstChild.nodeValue;

```
}

```

}  
scroller\_txt.spacebuffer = " ";  
scroller\_txt.border = true;  
scroller\_txt.speed = sped;

myTextFormat = new TextFormat();  
myTextFormat.font = “Verdana”; //this should be a string  
myTextFormat.color = black;  
myTextFormat.size = 13;

scroller\_txt.setTextFormat(myTextFormat);  
scroller\_txt.spaceSize = 50;//scroller\_txt.getNewTextFormat().getTextExtent(" ").width;  
scroller\_txt.spacesRequired = Math.ceil(scroller\_txt.\_width/scroller\_txt.spaceSize);  
for (var p = 0; p\<scroller\_txt.spacesRequired; p++) {  
scroller\_txt.spacebuffer += " ";

}

scroller\_txt.htmlText = scroller\_txt.spacebuffer+scroller\_txt.htmlText+scroller\_txt.spacebuffer;  
scroller\_txt.hscrollInterval = function() {  
if (this.hscroll == this.maxhscroll) {

this.hscroll = 0;  
}  
this.hscroll += 1;  
};  
setInterval(scroller\_txt, “hscrollInterval”, 50);

```
}
}

```

}

function you (filename)  
{

```
var i:String = filename;

```

}

public function view (filename):XML {

```
var xmlFile:XML = new XML(filename);

    xmlFile.load(filename);
    
    
    
    xmlFile.onLoad = function(bSuccess:Boolean):Void{

```

if (bSuccess){  
trace(“loading successful”);

}  
else {  
trace(“Doc failed to load or parse”);  
}

};

xmlFile.onLoad = init;  
xmlFile.load(filename);  
trace(“the init”+ xmlFile);  
return xmlFile;

}

//}  
}

[U].fla file

[/U]var filename:String = “E:\HELENA E stuff\Helena’s Game 10-23-2006\flash\datta.xml”;  
var craw:Crawler = new Crawler(filename);  
craw.view(filename);
