如何在Java中编写unicode字符0x{2}
?
我尝试使用"\u0002"
,但似乎无效。
我需要找到这个字符的原因是因为我需要在XML文件中替换它,然后才能解析它。
解析时提到的错误:An invalid XML character (Unicode: 0x{2}) was found in the value of attribute "{1}" and element is "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);
}
答案 0 :(得分:0)
我找到了。 Java中的0x{2}
错误消息中的"\u0004"
。替换它可以消除错误消息。
答案 1 :(得分:-1)
不允许使用该字符是XML,请参阅Wikipediate for ref:http://en.wikipedia.org/wiki/Valid_characters_in_XML#XML_1.0