Say! 머니곰/IT

XML : invalid characters

moneygom 2010. 6. 24. 19:23
반응형

XML 기반으로 작업하는 과정에서 유니코드의 특정 Character을 사용하는 경우
[XMLObject] unmarshal An invalid XML Character ( Unicode : 0x1 ) was found in the element content o the document.
등과 같은 유사한 문제가 발생할 수 있다.

예를 들면, 와 같은 문자이다. 이는 소문자 r 이 아닌 Unicode을 포함되는 다른 특정 문자입니다.


아래는 invalid character을 제거하는 소스 이다.
http://cse-mjmcl.cse.bris.ac.uk/blog/2007/02/14/1171465494443.html
/**

     * This method ensures that the output String has only valid
     * XML unicode characters as specified by the  XML 1.0 standard.
     * For reference, please see the
     * standard. This method will return an empty String if the input is null or empty.
     *
     * @author Donoiu Cristian, GPL

     * @param  The String whose non-valid characters we want to remove.

     * @return The in String, stripped of non-valid characters.

     */

    public static String removeInvalidXMLCharacters(String s) {

     // Used to hold the output.
        StringBuilder out = new StringBuilder();               

        // Used to reference the current character.
        int codePoint;                                         

        //This is actualy one unicode character, represented by two code units!!!.
        //String ss = "\ud801\udc00";                          

     //System.out.println(ss.codePointCount(0, ss.length()));// See: 1

  int i=0;

     while(i<s.length()) {

      System.out.println("i=" + i);

      //This is the unicode code of the character.
      codePoint = s.codePointAt(i);                      

            // Consider testing larger ranges first to improve speed.
   if ((codePoint == 0x9) ||                 

     (codePoint == 0xA) ||

     (codePoint == 0xD) ||

     ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||

     ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||

     ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {

    out.append(Character.toChars(codePoint));

   }    

   // Increment with the number of code units(java chars)
   // needed to represent a Unicode char.
   i+= Character.charCount(codePoint);                  

     }

     return out.toString();

    }

내용을 확인해 보면 Unicode 0x20 이전에는 0x9, 0xA, 0xD 을 제외한 값들은 사용할 수 없는 것을 뜻합니다.

* 그 외 처리 방법

Regex reg = new Regex(@"[\x00-\x08, \x0B-\x1F, \x7F]");         
Debug.WriteLine(reg.Replace(str," "));

반응형

'Say! 머니곰 > IT' 카테고리의 다른 글

캡쳐 프로그램, "오픈 캡쳐"  (0) 2010.06.24
알송 다운로드  (0) 2010.06.24
Unicode ( 유니코드 )  (0) 2010.06.23
Google Docs  (0) 2010.06.23
[알집] 알집 다운로드 및 설치방법  (0) 2010.06.22