2017-07-31 45 views
2

在AS3中我可以寫下面的(?):如何OpenFL(HAXE)通過XML導出(保存)數據

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}/>); 
     [email protected] = scene.objectArray[j].x; 
     [email protected] = scene.objectArray[j].y; 
     [email protected] = scene.objectArray[j].name; 
     [email protected] = scene.objectArray[j].band; 
     [email protected] = scene.objectArray[j].frame; 
     [email protected] = scene.objectArray[j].width; 
     [email protected] = scene.objectArray[j].height; 

     [email protected] = scene.objectArray[j].sprite; 
     [email protected] = 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

+0

你的Haxe目標是什麼? – Confidant

+0

HTML5是我使用Haxe時的主要目標 –

回答

1

你可以使用的FileReference閃存目標,並sys.ioFile爲支持的目標:

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

您可以使用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發送到某處)。

+0

這正是我所期待的,儘管我似乎無法在HTML5導出中充分發揮其作用。我有一個響應鼠標點擊的按鈕,該按鈕應該通過安全沙箱檢查(至少對於Flash),但是對話框不會彈出我想將XML文件保存在計算機上的位置( fileReference.save(root.toString(),「SomeXML.xml」)),現在已經很近了,但我感覺如此遙遠。有任何想法嗎? –

+0

如果您正在運行html,那麼您要查找的是html5 Blob api,並通過鏈接調用url。看到這個JS小提琴:https://jsfiddle.net/dvuyka/z8ouj1np/ – Chii

+0

這可以在Haxe項目中使用嗎? –