XML 规范化器问题

发布于 2024-10-19 16:30:23 字数 484 浏览 6 评论 0原文

我使用包 org.apache.xml.security.c14n 来规范化 XML。我使用以下代码:

private String CanonicalizeXML(String XML) throws InvalidCanonicalizerException, CanonicalizationException, ParserConfigurationException, IOException, SAXException {

    Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    return new String(canon.canonicalize(XML.getBytes()));
}

但是,它似乎没有按我的预期工作,因为它不会删除元素之间的任何不必要的空格。我做错了什么吗?

谢谢,

伊万

I'm using the package org.apache.xml.security.c14nfor the canonicalization of XMLs. I use the following code:

private String CanonicalizeXML(String XML) throws InvalidCanonicalizerException, CanonicalizationException, ParserConfigurationException, IOException, SAXException {

    Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    return new String(canon.canonicalize(XML.getBytes()));
}

However, it doesn't seem to work as I expected, since it doesn't delete any non-necessary white spaces between elements. Do I do something wrong?

Thanks,

Ivan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红墙和绿瓦 2024-10-26 16:30:23

我认为您的期望可能是不正确的:

您没有说 XML 规范化的版本,但 1.0 和 1.1 都说:

字符内容中的所有空格都是
保留(不包括删除的字符
在换行标准化期间)

I think it may be your expectation which is incorrect:

You don't say which version of XML Canonicalization, but both 1.0 and 1.1 say:

All whitespace in character content is
retained (excluding characters removed
during line feed normalization)

沉溺在你眼里的海 2024-10-26 16:30:23

您的 xml 文档是否引用了 dtd 或模式?如果没有其中之一,解析器就无法知道哪个空格是重要的,因此必须保留它。

Is your xml document referencing a dtd or schema? Without one of those the parser has no way to know which whitespace is significant and so it has to preservere it.

笨笨の傻瓜 2024-10-26 16:30:23

org.apache.xml.security.c14n 不会删除空格。

我通过在 SAXBuilder 上设置 setIgnoringBoundaryWhitespace = true 来解决:

SAXBuilder builder = new SAXBuilder ();
builder.setIgnoringBoundaryWhitespace(true);
org.jdom2.Document doc = builder.build(is);
DOMOutputter out = new DOMOutputter();
Document docW3 = out.output(doc);

The org.apache.xml.security.c14n does not remove whitespaces.

I resolved by setting setIgnoringBoundaryWhitespace = true on my SAXBuilder:

SAXBuilder builder = new SAXBuilder ();
builder.setIgnoringBoundaryWhitespace(true);
org.jdom2.Document doc = builder.build(is);
DOMOutputter out = new DOMOutputter();
Document docW3 = out.output(doc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文