【问题标题】:How to export (save) data in OpenFL (Haxe) via XML(?)如何通过 XML(?)在 OpenFL(Haxe)中导出(保存)数据
【发布时间】:2017-07-31 20:19:33
【问题描述】:

在 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

【问题讨论】:

  • 你的 Haxe 目标是什么?
  • HTML5 是我使用 Haxe 时的主要目标

标签: haxe openfl


【解决方案1】:

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

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>
    }
}

该示例中的 root.toString() 可以改为序列化为文件 File,或者实际上是任何其他类型的输出(例如通过 http 发布到某处)。

【讨论】:

  • 这正是我一直在寻找的,尽管我似乎无法让它在 HTML5 导出中充分发挥作用。我有一个响应鼠标单击的按钮,它应该通过安全沙箱检查(至少对于 Flash),但是没有弹出对话框,显示我想在我的计算机上保存 XML 文件的位置( fileReference.save(root.toString(),"SomeXML.xml")),现在如此接近,但我感觉如此遥远。有什么想法吗?
  • 如果您正在运行 html,那么您正在寻找的是 html5 Blob api,以及通过链接调用的 url。看到这个 JS 小提琴:jsfiddle.net/dvuyka/z8ouj1np
  • 这可以在 Haxe 项目中使用吗?
  • 是的,你可以写js目标特定的代码。 api.haxe.org/js/html/Blob.htmlapi.haxe.org/js/html/LinkElement.html 可能会让你开始(也许api.haxe.org/js/Browser.html 对你也有用)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多