java - 什么是 XML BOM 以及如何检测它?

标签 java xml

ANSI XML 文档中的 BOM 究竟是什么,应该将其删除吗? XML 文档应该使用 UTF-8 吗?谁能告诉我一个可以检测 BOM 的 Java 方法? BOM 由字符 EF BB BF 组成。

最佳答案

对于 ANSI XML 文件,它实际上应该被删除。如果你想使用 UTF-8,你真的不需要它。只有 UTF-16 和 UTF-32 才需要。

The Byte-Order-Mark (or BOM), is a special marker added at the very beginning of an Unicode file encoded in UTF-8, UTF-16 or UTF-32. It is used to indicate whether the file uses the big-endian or little-endian byte order. The BOM is mandatory for UTF-16 and UTF-32, but it is optional for UTF-8.

(来源:https://www.opentag.com/xfaq_enc.htm#enc_bom)

关于如何在java中检测到这个问题。

请检查此问题的以下答案:Java : How to determine the correct charset encoding of a stream如果您现在想自己确定 BOM(风险自负),例如查看此代码 Java Tip: How to read a file and automatically specify the correct encoding .

基本上只是自己读取前几个字节,然后确定您是否可能找到了 BOM。

关于java - 什么是 XML BOM 以及如何检测它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1772321/

相关文章:

Java Set of Maps hashCode 不正确?

java - 空指针异常 - 从 fragment 加载新 Activity

java - 从 web.xml 文件(CDATA 项)中删除 "error"标记

java - 如何使用 XPath 按 TextContent 过滤元素?按轴获取 parent ?

java - 如何在 Mono.subscribe 中使用 UI.getCurrent().navigate ("route")

java - JPA Hibernate OneToMany 枚举 - 未映射的类错误

将 Excel 日期数据保存到 MySQL 时,Java 日期格式化程序不起作用

java - Android volley 使用 intentservice 发送数据两次?

java - 如何用多行文本覆盖图像(文本将位于 Canvas 的中心)

java - FileNotFoundException : class path resource [WEB-INF/dispatcher-servlet. xml] 无法打开,因为它确实存在但不存在