特定字符在Java中无法正确呈现

时间:2014-04-28 14:01:01

标签: java xml swing encoding utf-8

在JTable中显示从服务器接收的字符串时出现问题。一些特定字符显示为小白色方块而不是“é”或“à”等。我尝试了很多东西,但没有一个能解决我的问题。我在Windows下使用Eclipse。服务器是使用Visual Studio 2010开发的。 服务器使用tinyXML2发送XML文件,客户端使用JDom来读取它。使用的字体是“Dialog”。服务器从Oracle数据库中获取字符串。

我认为这是一个编码问题,但我还没有解决它。

有没有人有想法?

THX

阿诺

编辑:根据要求,这就是我使用JDom的方式

public static Player fromXML(Element e)
{       
    Player  result  = new Player();
    String  e_text  = null;

    try
    {
        e_text = e.getChildText(XMLTags.XML_Player_playerId);
        if (e_text != null) result.setID(Integer.parseInt(e_text));

        e_text = e.getChildText(XMLTags.XML_Player_lastName);
        if (e_text != null) result.setName(e_text);

        e_text = e.getChildText(XMLTags.XML_Player_point_scored);
        if (e_text != null) result.addSpecial(STAT_SCORED, Double.parseDouble(e_text));

        e_text = e.getChildText(XMLTags.XML_Player_point_scored_last);
        if (e_text != null) result.addSpecial(STAT_SCORED_LAST, Double.parseDouble(e_text));
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }

    return result;
}

    public static Document load(String filename) {
    File XMLFile = new File(CLIENT_TO_SERVER, filename);
    SAXBuilder sxb = new SAXBuilder();
    Document document = new Document();
    try
    {
         document = sxb.build(new File(XMLFile.getPath()));
    } catch(Exception e){e.printStackTrace();}

    return document;
}

1 个答案:

答案 0 :(得分:0)

使用正确的编码读取文件,例如:

document = sxb.build(new BufferedReader(new InputStreamReader(new FileInputStream(XMLFile.getPath()),“UTF8”)));

注意:1。首先确定该文件中使用的字符编码。指定charset而不是上面的UTF8。

  1. Incase编码未知或者是从具有不同编码的各种系统生成的,您可以使用'Mozilla的编码检测器库'。 @see https://code.google.com/p/juniversalchardet/

  2. 需要处理UnsupportedEncodingException

相关问题