无法显示特殊字符

问题描述:

我无法在屏幕上显示特殊字符(抛光字符)。我有一个要求,我从数据库中获取具有一些特殊字符的数据。我以xml格式获取数据(xml没有将其识别为字符串)并将其传递给我尝试显示数据的操作。我试图将特殊字符的Uniciode设为ł 但是当我尝试显示时,这会转换为& amp ;#X142; 因此我无法显示它,因为它不会将其作为字符串。

I am unable to display special characters (polish characters) on screen. I have a requirement where I get the data from database which has some special characters. I get the data in an xml format (The xml is not recognizing it as a string) and pass it to action where I try to display the data. I am trying to get the Uniciode of the special character as ł but when I try to display, this gets converted to ł and so I am unable to display it because it does not take it as a string.

String ex1="ł";
System.out.println("ex1...."+ex1);
output:: ?

我试图使用以下代码获取Unicode ::

I am trying to get the Unicode using the following code::

    public static String convert (String str) throws UnsupportedEncodingException
    {
        String tc = str;
        String output = "";
        char[] ca = tc.toCharArray();
        for (int i = 0; i < ca.length; ++i) 
             {
               char a = ca[i];
               if ((int) a > 255) 
                    {
            output += "&"+"#X"+ Integer.toHexString((int) a) + ";";
               } 
                   else 
                   {
            output += a;
              }
        }
        return output;
    }

输出为:如果输入为 str =ł然后输出=&#X142;

The output is: If input is given as str="ł" then output=&#X142;

不要重新发明*!使用 StringEscapeUtils escapeXml 方法来自 Apache Commons Lang 库的/ a>类,它提供了这个简单的解决方案:

Don't reinvent the wheel! Use the escapeXml method of the StringEscapeUtils class from the Apache Commons Lang library, which makes for this simple solution:

StringEscapeUtils.escapeXml(input);