gpt4 book ai didi

java - JAXBContext 和 Marshaller 更改 XML namespace 的顺序

转载 作者:行者123 更新时间:2023-11-30 07:36:27 30 4
gpt4 key购买 nike

我有这样一行代码:

StringWriter writer = new StringWriter(); 
JAXBContext jc = JAXBContext.newInstance(namespace);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(input, writer);

namespace = "nfpa:nfpares" 时。我有一个这样的生成内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceRequest xmlns:ns2="nfpares" xmlns="nfpa">
...
</ServiceRequest>

但不知何故,在库的另一部分(我无法控制),开发人员也使用 JAXBContext 和相同的命名空间,但生成的内容是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceRequest xmlns="nfpa" xmlns:ns2="nfpares">
...
</ServiceRequest>

请注意 namespace 仍然相同,但它们的顺序已调换。我需要对原始内容进行加密验证。除了命名空间的顺序外,这两者之间的一切都是一样的。有谁知道发生了什么事?因为我使用了不同版本的 JAXBContext

谢谢

最佳答案

在对 xml 进行加密签名检查时,您需要使用规范形式的 xml。对于与 XML 解析器相同的相同 XML 结构,有许多不同的序列化可能性。例如<a/>对比<a></a> , &#160;对比&#xA0;或属性和命名空间的顺序。 XML canonicalization 有一个标准它描述了以下转换:

The canonical form of an XML document is physical representation of the document produced by the method >described in this specification. The changes are summarized in the following list:

  • The document is encoded in UTF-8
  • Line breaks normalized to #xA on input, before parsing
  • Attribute values are normalized, as if by a validating processor
  • Character and parsed entity references are replaced
  • CDATA sections are replaced with their character content
  • The XML declaration and document type declaration (DTD) are removed
  • Empty elements are converted to start-end tag pairs
  • Whitespace outside of the document element and within start and end tags is normalized
  • All whitespace in character content is retained (excluding characters removed during line feed normalization)
  • Attribute value delimiters are set to quotation marks (double quotes)
  • Special characters in attribute values and character content are replaced by character references
  • Superfluous namespace declarations are removed from each element
  • Default attributes are added to each element
  • Lexicographic order is imposed on the namespace declarations and attributes of each element

此方法的实现可以在 apache xml security project 中找到在类里面Canonicalizer

关于java - JAXBContext 和 Marshaller 更改 XML namespace 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3589093/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com