【问题标题】:Actionscript/flex3: Escape XML special characters like <, >, " and ' inside/within tag contentsActionscript/flex3:转义 XML 特殊字符,如 <、>、" 和 ' inside/within 标签内容
【发布时间】:2013-08-30 10:23:05
【问题描述】:

有没有办法通过仅将 XML 处理为字符串来转义 XML 标记内容中的 XML 特殊字符?可以使用正则表达式(regexp)来完成吗? 尝试从字符串创建新 XML() 时出现格式错误的 XML 运行时错误,因为它在某些标记内包含“

【问题讨论】:

标签: xml regex apache-flex actionscript string-parsing


【解决方案1】:

你可以这样使用:

 public static function escapeXMLTagContents(a_string:String):String{
        var l_indexOfSpecialChar:int = -1,
            l_tagsMatch:RegExp =/<(\?|[a-zA-Z_]{1}|\/{1})[^<]*?>/g,
            l_tags:Array = [],
            l_tagCharacterIndexes:Array = [],
            l_stringCopy:String = new String(a_string),
            i:int = -1,
            l_replaceArray:Array = [],
            l_return:String = "",
            l_tagCharIndex:int = -1,
            l_replaceChar:String = "";

        l_replaceArray.push("&|&amp;");
        l_replaceArray.push("<|&lt;");
        l_replaceArray.push(">|&gt;");
        l_replaceArray.push("\"|&quot;");
        l_replaceArray.push("'|&apos;");

        l_tags = a_string.match(l_tagsMatch);
        i = l_tags.length;
        while (--i > -1){
            var l_tagText:String = l_tags[i];
            var l_startIndex:int = l_stringCopy.lastIndexOf(l_tagText);
            var l_endIndex:int = l_startIndex + (l_tagText.length - 1);

            for (var j:int = l_startIndex; j <= l_endIndex; j++){
                if(l_tagCharacterIndexes.indexOf(j) < 0){
                    l_tagCharacterIndexes.push(j);
                }
            }

            l_stringCopy = l_stringCopy.substring(0, l_startIndex);
        }

        l_return = new String(a_string);
        for each (l_replaceChar in l_replaceArray){
            l_stringCopy = new String(l_return);
            while ((l_indexOfSpecialChar = l_stringCopy.lastIndexOf(l_replaceChar.charAt(0))) > -1) {
                // determine if it char needs to be escaped (i.e is inside tag contents)
                if(l_tagCharacterIndexes.indexOf(l_indexOfSpecialChar) == -1){
                    l_return = l_return.substring(0, l_indexOfSpecialChar) + l_replaceChar.split("|")[1] + l_return.substring(l_indexOfSpecialChar+1);

                    // adjust indexes
                    for (i = 0; i < l_tagCharacterIndexes.length; i++) {
                        l_tagCharIndex = l_tagCharacterIndexes[i];
                        if(l_tagCharIndex >= l_indexOfSpecialChar) {
                            l_tagCharacterIndexes[i] = l_tagCharacterIndexes[i] + String(l_replaceChar.split("|")[1]).length-1; // -1 from the old characther "&,<,>," or '"
                        }
                    }
                }

                l_stringCopy = l_stringCopy.substring(0, l_indexOfSpecialChar);
            }
        }

        return l_return;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2023-03-27
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多