2012-06-08 59 views
0

我似乎無法讓我的xml在節點內生成。它每次重複整個xml結構。用例是這個文件將會在幾個小時/天內重複更新。這個XML文件也連接到一個後端,所以我保持這種格式是至關重要的。在根節點內更新xml

目前xml結尾這樣看:

<xmlcontainer> 
    <details> 
    <name>name</name> 
    <phone>phone</phone> 
    <email>email</email> 
    <image>0</image> 
    <image>1</image> 
    </details> 
</xmlcontainer><xmlcontainer> 
    <details> 
    <name>name2</name> 
    <phone>phone2</phone> 
    <email>email2</email> 
    <image>3</image> 
    <image>4</image> 
    </details> 
</xmlcontainer> 

當它應該是這樣的。

<xmlcontainer> 
    <details> 
    <name>name</name> 
    <phone>phone</phone> 
    <email>email</email> 
    <image>0</image> 
    <image>1</image> 
    </details> 
    <details> 
    <name>name2</name> 
    <phone>phone2</phone> 
    <email>email2</email> 
    <image>3</image> 
    <image>4</image> 
    </details> 
</xmlcontainer> 

以下是我的代碼,跨越2個框架。我錯過了什麼讓這發生? 我搞砸了fs.open(fl2,FileMode.APPEND);和fs.open(fl2,FileMode.WRITE);但它似乎沒有什麼大不了。

感謝

//frame 2 
var xml_file = File.documentsDirectory.resolvePath('app/information.xml').nativePath; 

    var xml_created = false; 
    var xml_data = ""; 

    function load_xml(f_name){ 
     var target_url:URLRequest = new URLRequest(f_name); 
     var target_load:URLLoader= new URLLoader(); 
     var xmlData : XML = new XML(); 
     target_load.load(target_url); 
     target_load.addEventListener(Event.COMPLETE, target_complete); 
     target_load.addEventListener(IOErrorEvent.IO_ERROR, target_error); 

     function target_complete (evt:Event):void { 
     xmlData = new XML(evt.target.data); 
     xml_created = true; 
     xml_data = String(xmlData.details); 
     nextFrame(); 
     trace("OK - load XML") 
     } 

     function target_error(evt:IOErrorEvent):void { 
     nextFrame(); 
     trace("ERROR - load xml - check xml file name"); 
     } 

    } 

    load_xml(xml_file); 

//frame 5 
function saveFiles_fun(){ 
    var xmlData:XML = new XML(<xmlcontainer> 
    </xmlcontainer> 
); 

    var details:XML = new XML(<details/>) 
    //client.appendChild(<id/>); 
    details.appendChild(new XML("<name>qwe</name>")); 
    details.appendChild(new XML("<phone>qwe</phone>")); 
    details.appendChild(new XML("<email>qwe</email>")); 

    for (var fna:int = 0; fna<fileNameArray.length; fna++) { 
    details.appendChild(new XML("<image>" + fna + "</image>")); 
    } 

    xmlData.appendChild(details); 

    var fl1:File = File.applicationDirectory.resolvePath(xml_file); 
    var fl2:File = new File(fl1.nativePath); 

    var fs:FileStream = new FileStream(); 
    try{ 
    fs.open(fl2,FileMode.APPEND); 
    fs.writeUTFBytes(xmlData); 
    fs.close(); 
    doSomething(); 
    }catch(e:Error){} 
} 


function doSomething(){ 
form.alert.text = alert4; 
var _tim5 = setTimeout(gotoAndPlay, 1000,1); 
} 

回答

1

它看起來對我來說,你的問題是與這條線。

fs.open(fl2,FileMode.APPEND); 

你爲什麼不只是完全重新創建該文件,然後通過現有的文件寫呢?


FileMode.APPEND 

指定該文件是開放的追加。如果 它不存在,則創建該文件。如果文件存在,則覆蓋現有數據不是 ,並且所有寫入都從文件末尾開始。


你需要使用什麼是

FileMode.WRITE



你也應該不是你的嵌套函數,這將導致問題的你,如果你讓它一個普遍的做法。

[更新]

// in the code you supplied find this line 
xmlData.appendChild(details); 

// and right above it add this line 
xmlData.appendChild(xml_data); 

// so the resulting code will look like this 
xmlData.appendChild(xml_data); // xml_data is where the original document is stored 
xmlData.appendChild(details); 

// also include the change to 
fs.open(fl2,FileMode.WRITE); 
+0

我希望你沒有支付這個代碼的最高美元。它根本不是乾淨的代碼。 –

+0

我不想進入。這整個事情是一個痛處。 – user1004277

+0

試着將APPEND改爲WRITE,看看你得到了什麼。 –

0

從您的 「saveFiles_fun」 的方法獲取調用?我想你正在使用此代碼段創建XML的新實例

var xmlData:XML = new XML( );

然後你添加childXML。