2013-08-27 3 views
2

XML을 문자열로만 처리하여 XML 태그 내용에 XML 특수 문자를 이스케이프 (escape) 할 수 있습니까? ?이 일부 태그 내부에 "<"문자가 포함되어 있기 때문에 문자열에서 새로운 XML()를 만들려고 할 때 (정규 표현식) 나는 잘못된 XML의 런타임 오류가Actionscript/flex3 : <, >과 같은 XML 특수 문자를 이스케이프 처리하고 "/"태그 내용의 내부/내부

+0

CDATA를 사용해 보셨습니까? http://www.w3schools.com/xml/xml_cdata.asp – divillysausages

+0

@ [divillysausages] (http://stackoverflow.com/users/639441/divillysausages) : 대답은 서버 코드를 수정할 수있는 권한이 없거나 서버 코드를 수정하지 않으려는 경우 클라이언트 측 코드 (수정)를 대상으로합니다. 서버 코드. – OviC

답변

2

당신은 같은 것을 사용할 수 있습니다.

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; 
    }