我浏览了一些帖子,例如 FileReader reads the file as a character stream和 can be treated as whitespace if the document is handed as a stream of characters其中答案表明输入源实际上是字符流,而不是字节流。
但是,1 中建议的解决方案似乎不适用于 UTF-16LE。虽然我使用这段代码:
try (final InputStream is = Files.newInputStream(filename.toPath(), StandardOpenOption.READ)) {
DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.parse(new InputSource(is));
return parser.getDocument();
} catch (final SAXParseException saxEx) {
LOG.debug("Unable to open [{}}] as InputSource.", absolutePath, saxEx);
}
我仍然得到org.xml.sax.SAXParseException: Content is not allowed in prolog.
.
我查看了 Files.newInputStream,它确实使用了 ChannelInputStream
它将传递字节,而不是字符。我还尝试设置 InputSource 对象的编码,但没有成功。
我还检查了 <?xml
之前没有多余的字符(BOM 除外)部分。
我还想提一下,这段代码在 UTF-8 下工作得很好。
//编辑: 我还尝试了 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse() 和 XmlInputStreamReader.next(),结果相同。
//编辑2: 尝试使用缓冲阅读器。相同的结果: 序言中出现意外字符“뿯”(代码 49135/0xbfef);预期 '<'
提前致谢。
最佳答案
为了进一步收集一些信息:
byte[] bytes = Files.readAllBytes(filename.toPath);
String xml = new String(bytes, StandardCharsets.UTF_16LE);
if (xml.startsWith("\uFEFF")) {
LOG.info("Has BOM and is evidently UTF_16LE");
xml = xml.substring(1);
}
if (!xml.contains("<?xml")) {
LOG.info("Has no XML declaration");
}
String declaredEncoding = xml.replaceFirst("<?xml[^>]*encoding=[\"']([^\"']+)[\"']", "$1");
if (declaredEncoding == xml) {
declaredEncoding = "UTF-8";
}
LOG.info("Declared as " + declaredEncoding);
try (final InputStream is = new ByteArrayInputStream(xml.getBytes(declaredEncoding))) {
DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.parse(new InputSource(is));
return parser.getDocument();
} catch (final SAXParseException saxEx) {
LOG.debug("Unable to open [{}}] as InputSource.", absolutePath, saxEx);
}
关于java - UTF-16LE 编码和 xerces2 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57870212/