views:

1584

answers:

3

Hello, I'm just trying to load an xml file witch can be anywere in the hdd, this is what I have done to browse it, but later when I'm trying to load the file it would only look in the same path of the swf file

here is the code

package { import flash.display.Sprite; import flash.events.; import flash.net.;

public class cargadorXML extends Sprite {


 public var cuadro:Sprite = new Sprite();
 public var file:FileReference;
 public var req:URLRequest;
 public var xml:XML;
 public var xmlLoader:URLLoader = new URLLoader();

 public function cargadorXML() {
  cuadro.graphics.beginFill(0xFF0000);
  cuadro.graphics.drawRoundRect(0,0,100,100,10);
  cuadro.graphics.endFill();
  cuadro.addEventListener(MouseEvent.CLICK,browser);
  addChild(cuadro);

 }
 public function browser(e:Event) {

  file = new FileReference();
  file.addEventListener(Event.SELECT,bien);
  file.browse();

 }
 public function bien(e:Event) {
  xmlLoader.addEventListener(Event.COMPLETE, loadXML);
  req=new URLRequest(file.name);
  xmlLoader.load(req);
 }
 public function loadXML(e:Event) {
  xml=new XML(e.target.data);
  //xml.name=file.name;
  trace(xml);
 }
}

}

when I open a xml file that isnt it the same directory as the swf, it gives me an unfound file error. is there anything I can do? cause for example for mp3 there is an especial class for loading the file, see http://www.flexiblefactory.co.uk/flexible/?p=46

thanks

A: 

Since you're loading a local xml file, you'll want to use FileStream.read() rather than a URLRequest. Also note that file.name will just give you the name of the file, not the full path, so what you'll want to do is:

 public function bien(e:Event) {
    var file:File = File.documentsDirectory.resolvePath(e.target);
    var fileStream:FileStream = new FileStream();
    fileStream.open(file, FileMode.READ);
    var prefsXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
    fileStream.close();
}

You may want to read the FileStream reference (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/filesystem/FileStream.html), if you're opening a large xml file, you'll need to read it asynchronously.

quoo
+1  A: 

Note that that answer only works for AIR applications!

A: 
var cuadro:Sprite = new Sprite();
var file:FileReference;
var xml:XML;

function cargadorXML() {
    cuadro.graphics.beginFill(0xFF0000);
    cuadro.graphics.drawRoundRect(0,0,100,100,10);
    cuadro.graphics.endFill();
    cuadro.addEventListener(MouseEvent.CLICK,browser);
    addChild(cuadro);

}
function browser(e:Event) {

    file = new FileReference();
    file.addEventListener(Event.SELECT,bien);
    file.browse();

}
function bien(e:Event) {
    file.addEventListener(Event.COMPLETE, loadXML);
    file.load();
}
function loadXML(e:Event) {
    xml = new XML(e.target.data);
    trace(xml);
}

cargadorXML();

Loading a local XML file. It might help someone...

Donovan