views:

165

answers:

3

All I can find information on for the URLLoader object in Actionsript 3.0 involves loading XML files, which I don't want to do. I'm trying to load in a .txt file that I want to parse, line by line with each line being delimited by a comma. Anyone know a method of doing this or a place where I can find some information on how to do this? Thanks!

A: 

Try this:

http://www.tinypixels.co.uk/products/flash/SimpleCsvLoader.php

dhdean
Well I'm not really interested in buying someone elses code, all I need to do is find a way to get the data out of the URLLoader object once it's loaded :(
Joe.F
How about this?http://flex.sys-con.com/node/45619
dhdean
That looks great, that will also help a lot once I have figured out how to load the file. Thanks!
Joe.F
+2  A: 

Look at the String's split() methods for start. It splits a string into an array based on a delimiter. In your case you would access each line as an element of the array returned by using split() and the comma character.

e.g.

var csvLoader:URLLoader = new URLLoader(new URLRequest('yourFile.csv'));
csvLoader.addEventListener(Event.COMPLETE, csvLoaded);

function csvLoaded(event:Event):void{
   var lines:Array = String(event.target.data).split(',');
   var linesNum:int = lines.length;
   for(var i:int = 0 ; i < linesNum; i++){
      trace('line ' + i + ': ' + lines[i]);
   }
}

You can use event.target.data.split(','), I used String to make split()'s origin obvious.

@dhdean's tutorial is pretty cool, bare in mind that is as2.0, so there are slight differences with loading the file, but parsing strings should be pretty much the same in as2.0/as3.0

Depending on your comfort level with as3.0 you might want to have a look at csvlib.

HTH, George

George Profenza
That's great code, thanks. I think my problem ended up being that apparently, for whatever reason, URLLoader isn't loading my file at all. I figured it was loading and couldn't find the text anywhere, so...yeah. Now I get to figure out why it's not loading, that code will come in helpful though once I figure out how to load the data - thanks!
Joe.F
@Joe.F Glad u got the loading sorted out. Probably as a best practice you should add event listeners for IOErrorEvent, SecurityEvent and maybe HTTPStatusEvent. Usually if the loading fails you will get an error, or if you listen for the IOErrorEvent, you can check the url again, typos and relative/absolute paths are common mistakes.
George Profenza
A: 

For any example that you see that works with XML files, you just have to delete the line that says "new XML()"

For example, in this example at http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoader.html#load()

You have the following example:

   private function loaderCompleteHandler(event:Event):void {

            try {
                externalXML = new XML(loader.data);
                readNodes(externalXML);    
            } catch (e:TypeError) {
                trace("Could not parse the XML file.");
            }
    }

Your text data is in the variable "loader.data"

Using

var arr:Array = loader.data.split(",");

will return an array that was delimited by a comma.

teehoo
That's a really simple way of looking at it, thanks!
Joe.F