我正在AS3 / Flex中为AIR应用程序加载一个xml文件。当我保存它时,缩进看起来很漂亮,但是原始文件中的所有选项卡现在都是空格。我可以保存标签吗?
打开文件代码:
var file:File = event.target as File;
var fileStream:FileStream = new FileStream();
fileStream.open( file, FileMode.READ );
var fileContents:String = fileStream.readUTFBytes(
fileStream.bytesAvailable );
fileStream.close();
XML.ignoreComments = false;
XML.ignoreWhitespace = false;
var newXML:XML = new XML(fileContents);
var scriptParent:XML = <xml></xml>;
scriptParent.appendChild(newXML);
data = new XMLListCollection(scriptParent.children());
保存文件代码:
var stream:FileStream = new FileStream();
stream.open(currFileObject, FileMode.WRITE);
stream.writeUTFBytes(XML (data));
stream.close();
谢谢!
答案 0 :(得分:1)
我最终转换为字符串,只是用tab替换双空格。我找到了Colin Moock的替换功能。
function saveData(event:Event):void
{
var newFile:File = event.target as File;
var stream:FileStream = new FileStream();
stream.open(newFile, FileMode.WRITE);
var xString:String = data.toXMLString();
var result = replace(xString, " ", String.fromCharCode(9));
stream.writeUTFBytes( (result));
stream.close();
}
function replace (origStr, searchStr, replaceStr) {
var tempStr = "";
var startIndex = 0;
if (searchStr == "") {
return origStr;
}
if (origStr.indexOf(searchStr) != -1) {
var searchIndex = origStr.indexOf(searchStr, startIndex)
while ( searchIndex != -1) {
tempStr += origStr.substring(startIndex, searchIndex);
tempStr += replaceStr;
startIndex = searchIndex + searchStr.length;
searchIndex = origStr.indexOf(searchStr, startIndex)
}
return tempStr + origStr.substring(startIndex);
} else {
return origStr;
}
}