我似乎无法让我的 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);
}