如何用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);
}
参考方案
我找到了。错误消息中的0x{2}
在Java中是"\u0004"
。更换可以消除错误消息。
有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java:BigInteger,如何通过OutputStream编写它 - java我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…
Java:从类中查找项目名称 - java仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…
Java Double与BigDecimal - java我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…