java - 用Java编写unicode 0x2

标签 java unicode character-encoding

如何在 Java 中编写 unicode 字符 0x{2}

我尝试使用 "\u0002" 但似乎不起作用。

我需要找到这个字符的原因是因为我需要在 XML 文件中替换它,然后才能解析它。

我在解析时遇到的错误提到:在属性“{1}”的值中发现无效的 XML 字符(Unicode:0x{2}),元素为“4”。替换 \u0002 无法解决该错误。

这就是我解析的方式:

try {
    // Fixing any invalid characters in the XML file
    fixXMLFile(xmlFile);

    // Get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();

    // Get a new instance of parser
    SAXParser sp = spf.newSAXParser();

    // Parse the file and also register this class for call backs
    sp.parse(xmlFile, this);

} catch(Exception e) {
    System.out.println(e.getLocalizedMessage());
}

修复方法:

private void fixXMLFile(File xmlFile) throws IOException {
    File tempFile = File.createTempFile("dont_delete", ".tmp");
    FileWriter fw = new FileWriter(tempFile);

    Reader fr = new FileReader(xmlFile);
    BufferedReader br = new BufferedReader(fr);

    int sdds = 0;
    while(br.ready()) {
        String tmp = br.readLine();
        if (tmp.contains("\u0002")) System.out.println(++sdds);
        fw.write(tmp.replaceAll("\u0002", "") + "\n");
    }

    fw.close();
    br.close();
    fr.close();

    // Finally replace the original file.
    tempFile.renameTo(xmlFile);
}

最佳答案

我找到了。错误消息中的0x{2}在Java中是“\u0004”。替换它可以消除错误消息。

关于java - 用Java编写unicode 0x2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396581/

相关文章:

delphi - RAR 文件中的非 ASCII 文件名是如何编码的?

php - 页面上图像之间存在空格,但无法在 HTML/CSS 中找到源代码

java - 从 try 资源结构返回 Future

java - Spring 启动: getting this error - Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

Python 没有正确排序 unicode。 Strcoll 没有帮助

python - 通过 scrapy 抓取数据的 unicode 问题

html - 阿拉伯文字显示不正确

java - principal.getName() 返回 facebook 的 ID 而不是名称

java - HashTagHelper Creator() 具有私有(private)访问权限吗?

php - Unicode 字符未反射(reflect)在 MySql 和 HTML 中