java - UTF-16LE 编码和 xerces2 Java

标签 java xml utf-16 xerces byte-order-mark

我浏览了一些帖子,例如 FileReader reads the file as a character streamcan 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/

相关文章:

java - 使用 Spring EL 将值设置为 lambda

c# - 使用 XMLDocument C# xml 添加多个节点

java - 给定字节数的最大字符串长度

python - 解码 UTF-16 编码的十六进制字符的字符串

java - 在 Spring MVC 中,使用 'redirect:process' 或 'redirect:/process' 在导致丢失 session 或 session 值方面是否有所不同?

java - 如何将 HTML 表数据(从每一行)获取到 servlet 中

java - 使用 JOOQ,如何将 SQLException 映射到业务异常?

java - Leetcode 子数组最小值之和通过 86/87 测试。可能是我的模计算有问题

java - 将数据对象从 xml 发送到 viewmodel

Python3逐行读取混合文本/二进制数据