如何通过XML(?)在OpenFL(Haxe)中导出(保存)数据

时间:2017-07-31 20:19:33

标签: haxe openfl

在AS3中我可以写下以下内容:

fileReference = new FileReference();
var xmlStage:XML = new XML(<STAGE/>);
var xmlObjects:XML = new XML(<OBJECTS/>);
var j:uint;
var scene:SomeScene = ((origin_ as SecurityButton).origin as SomeScene);
var object:SomeObject;

for (j = 0; j < scene.objectArray.length; ++j) {
    object = scene.objectArray[j];
    if (1 == object.saveToXML){
        var item:String = "obj";
        var o:XML = new XML(<{item}/>);
        o.@x = scene.objectArray[j].x;
        o.@y = scene.objectArray[j].y;
        o.@n = scene.objectArray[j].name;
        o.@g = scene.objectArray[j].band;
        o.@f = scene.objectArray[j].frame;
        o.@w = scene.objectArray[j].width;
        o.@h = scene.objectArray[j].height;

        o.@s = scene.objectArray[j].sprite;
        o.@b = scene.objectArray[j].bodyType;
        xmlObjects.appendChild(o);
        //System.disposeXML(o);
    }
}

xmlStage.appendChild(xmlObjects);
fileReference.save(xmlStage, "XML.xml");
//System.disposeXML(xmlObjects);
//System.disposeXML(xmlStage);
//fileReference = null;

在Haxe中是否有相同的方法可以做到这一点? (感兴趣的目标: HTML5

如果没有,我的选择是什么?

(AS3中此代码的导出结果显示在下面的链接中)

https://pastebin.com/raw/5twiJ01B

2 个答案:

答案 0 :(得分:2)

您可以使用Xml类创建xml(请参阅示例:https://try.haxe.org/#68cfF

openMapPage()
  {


   var ref = firebase.database().ref("request");
    ref.once("value").then((snapshot) => { // <------ Here!
        var a = snapshot.exists();  // true
        var c = snapshot.hasChild("reqdetails"); // true
        var d = snapshot.child('reqdetails').exists();
        var requestsKey = snapshot.key;
        var requestsValue = snapshot.val();

        snapshot.forEach((childSnapshot) => { // <------ And here!
            var requestKey = childSnapshot.key;
            var requestValue = childSnapshot.val();

            var reqdetails = requestValue.reqdetails;
            if (reqdetails) {
                this.data = requestKey;
                console.log(this.data);
                //this.arr.push(requestKey);
                //console.log(this.arr);
                 this.getRequest = this.angFire.list('request', {
                   query: {
                   orderByChild: 'reqdetails',
                   startAt: 'reqdetails'
            }
         })   

            }
        });
/*        this.navCtrl.push(RequestsPage, {
            thing1: this.data
         });*/

        //this.navCtrl.push(RequestsPage, { 'param1': this.data });
    });     
  }

该示例中的class Test { static function main() { var root = Xml.createElement('root'); var child = Xml.createElement('my-element'); child.set('attribute1', 'value1'); //add your own object's values child.set('attribute2', 'value2'); //may be add a few more children root.addChild(child); //this could be a file write, or POST'ed to http, or socket trace(root.toString()); // <root><my-element attribute1="value1" attribute2="value2"/></root> } } 可以被序列化为文件File,或者实际上任何其他类型的输出(例如通过http POST到某处)。

答案 1 :(得分:1)

您可以将FileReference用于Flash目标,将sys.ioFile用于支持的目标:

    var output = sys.io.File.write(path, true);
    output.writeString(data);
    output.flush();
    output.close();