Java:XML 规范化

发布于 2024-10-19 17:53:42 字数 1087 浏览 7 评论 0原文

在 Java 中制作规范形式的 XML 文件的最简单方法是什么?你有一些完成的代码吗?我在网上找到了几个链接,例如 这个这个,以及这个,但我无法让它工作:/

谢谢,

伊万

编辑:我使用了那里提出的规范化器,但我得到了奇怪的结果。更准确地说,此方法不会删除元素之间的空格...这就是我得到的:

<Metric xmlns="http://www.ibm.com/wsla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="total_memory_consumption_metric" type="double" unit="Mbit" xsi:schemaLocation="http://www.ibm.com/wsla WSLA.xsd">                        <Source>ServiceProvider</Source>                        <MeasurementDirective resultType="double" xsi:type="StatusRequest">                              <RequestURI> ***unused*** </RequestURI>                        </MeasurementDirective>                  </Metric>

What's the easiest way to make a canonical form of a XML file in Java? Do you have some done code for that? I've found several links on the net, like this, this, and this, but I can't make it to work :/

Thanks,

Ivan

EDIT: I used the canonicalizer that was proposed down there, but I get strange results. To be more precize, this method doesn't delete white spaces between elements... This is what I get:

<Metric xmlns="http://www.ibm.com/wsla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="total_memory_consumption_metric" type="double" unit="Mbit" xsi:schemaLocation="http://www.ibm.com/wsla WSLA.xsd">                        <Source>ServiceProvider</Source>                        <MeasurementDirective resultType="double" xsi:type="StatusRequest">                              <RequestURI> ***unused*** </RequestURI>                        </MeasurementDirective>                  </Metric>

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

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

发布评论

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

评论(2

静赏你的温柔 2024-10-26 17:53:42

Canonicalizer 类位于 Apache XML 安全项目

初始化库。

org.apache.xml.security.Init.init(); 

转换您的 XML。

Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
byte canonXmlBytes[] = canon.canonicalize(yourXmlBytes);
String canonXmlString = new String(canonXmlBytes);

The Canonicalizer class at Apache XML Security project.

Initialize the library.

org.apache.xml.security.Init.init(); 

Convert your XML.

Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
byte canonXmlBytes[] = canon.canonicalize(yourXmlBytes);
String canonXmlString = new String(canonXmlBytes);
余生共白头 2024-10-26 17:53:42

另一个选择是 nu.xom.canonical.Canonicalizer 如果您正在使用 XOM,或者如果您不需要 Apache XML 安全性。

Another option is nu.xom.canonical.Canonicalizer if you're using XOM, or if you don't otherwise have a need for Apache XML Security.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文