简体   繁体   English

在AS3 / FLex4 / Air应用程序中保存XML时保留选项卡

[英]Preserve tabs when saving XML in AS3/FLex4/Air app

I'm loading an xml file in AS3/Flex for an AIR app. 我正在AS3 / Flex中为AIR应用程序加载xml文件。 When I save it, the indenting looks pretty, however all the tabs from the original file, are now spaces. 当我保存它时,缩进看起来很漂亮,但是原始文件中的所有选项卡现在都是空格。 Can I save to that the tabs remain? 我可以保存这些标签吗?

open file code: 打开文件代码:

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());

save file code: 保存文件代码:

var stream:FileStream = new FileStream();
stream.open(currFileObject, FileMode.WRITE);
stream.writeUTFBytes(XML (data));
stream.close();

thanks! 谢谢!

I ended up converting to a string and just replacing double spaces with tab. 我最终转换为字符串,只是用tab替换了双空格。 I found a replace function by Colin Moock. 我发现了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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM