user2852398
user2852398

Reputation: 43

How to load xml in as3, air for android

I'm making an app in air for android in flash and I want some text to apear, so I thought that using xml is an good way to do this but I figured out that this is not as simple as I thought. I hade an code but that was for as2 and didn't worked so my question is does someone has an good working code to load some text out of an xml file or does someone know a better way to load text in flash??? thanks for the reply

Upvotes: 1

Views: 1239

Answers (1)

pulterao
pulterao

Reputation: 160

The code below loads a xml file from your application directory ( You have to add the xml file to your application directory) in to the _myXml property.

private var _myXml : XML;
private var _file : File;

public function loadXML() : void {
  _file = File.applicationDirectory.resolvePath("myXml.xml");
  if (_file.exists) {     
    var stream : FileStream = new FileStream();
    stream.open(_file, FileMode.READ);
    var str : String = stream.readUTFBytes(stream.bytesAvailable);
    stream.close();
   _myXml = new XML(str);
  } else {
    trace("WARNING file:" +_file.nativePath + " does not exist");
  } 
}

This code shows how you can actually work with the XML file and extract data from it

public function loadScenes(lang : String) : Vector.<Scene> {
var scenes : Vector.<Scene> = new Vector.<Scene>();

for each (var scene : XML in _myXml.children()) {
    var sc : Scene = new Scene(loadImage(scene.background.@imageName, scene.background.@width, scene.background.@height), LibraryManager.getFurnitureById(scene.furniture.@furnitureId),scene.furniture.@furnitureId,lang);
    sc.furniture.x = scene.furniture.@x;
    sc.furniture.y = scene.furniture.@y;
    sc.furniture.scaleX = scene.furniture.@scaleX;
    sc.furniture.scaleY = scene.furniture.@scaleY;
    sc.furniture.rotation = scene.furniture.@rotation;
    sc.furniture.gotoAndStop(scene.furniture.@currentFrame);
    sc.setup = true;
    scenes.push(sc);
}
return scenes;

}

my xml looks like this

<scenes>
      <scene>
        <furniture furnitureId="13" currentFrame="1" rotation="0" scaleY="0.4021450653932559" scaleX="0.4021450653932559" y="510.7" x="468.7" id="1"/>
        <background height="640" width="980" imageName="Garden2"/>
      </scene>
      <scene>
        <furniture furnitureId="8" currentFrame="1" rotation="0" scaleY="0.5015106201171875" scaleX="0.5015106201171875" y="516.9" x="488.55" id="2"/>
        <background height="640" width="980" imageName="Garden3"/>
      </scene>
      <scene>
        <furniture furnitureId="15" currentFrame="1" rotation="-0.06605712343630953" scaleY="0.4068730437596716" scaleX="0.4068730437596716" y="454.85" x="518.5" id="1"/>
        <background height="640" width="980" imageName="Garden1"/>
      </scene>
    </scenes>

Upvotes: 1

Related Questions