我希望根据 Canonical XML Spec 对 JAXB 解码的 XML 进行规范化
如果我这样写:
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
它会起作用吗?
如果没有,JAXB 可以完成这项工作吗?
最佳答案
以下是我的初步答案:http://www.w3.org/TR/2001/REC-xml-c14n-20010315#Terminology
1 - The document is encoded in UTF-8
默认情况下,JAXB 编码为 UTF-8 编码。
2 - Line breaks normalized to #xA on input, before parsing
JAXB 不保留换行符,因此这并不真正适用。
3 - Attribute values are normalized, as if by a validating processor
您可以指定XmlSchema
关于Unmarshaller
要让 JAXB 实现使用验证处理器:
4- Character and parsed entity references are replaced
大多数 JAXB 实现将这项工作委托(delegate)给所使用的底层解析器。
5- CDATA sections are replaced with their character content
标准 JAXB API 不允许您编码到 CDATA 部分,因此您可以在这里使用。
6 - The XML declaration and document type declaration (DTD) are removed
JAXB 不写出 DTD 声明。您可以通过执行以下操作删除 XML 声明:
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
7 - Empty elements are converted to start-end tag pairs
JAXB 不会将空元素写为开始-结束标记对。应该能够想出一个解决方法。
8 - Whitespace outside of the document element and within start and end tags is normalized
9 - All whitespace in character content is retained (excluding characters removed during line feed normalization)
JAXB 实现保留字符内容中的所有空格(开始/元素标记之间)。
10 - Attribute value delimiters are set to quotation marks (double quotes)
引用和 MOXy JAXB 实现使用双引号作为属性值分隔符。
11 - Special characters in attribute values and character content are replaced by character references
JAXB 将取代 &
与 &
, <
与 <
,和"
与 "
12 - Superfluous namespace declarations are removed from each element
JAXB 实现尽力不编写额外的命名空间声明,但不能保证不声明额外的命名空间。您可以采取一些解决方法来解决此问题。
13 - Default attributes are added to each element
待定
14 - Lexicographic order is imposed on the namespace declarations and attributes of each element
JAXB 实现不保证每个元素的 namespace 声明和属性的顺序。
关于java - JAXB 和 XML 规范化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433590/