Java:XML规范化 [英] Java: XML canonicalization

查看:154
本文介绍了Java:XML规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :/

谢谢,

Ivan

编辑:我使用了规范化程序那是在那里提出的,但我得到了奇怪的结果。为了更加精确,这种方法不会删除元素之间的空格......这就是我得到的:

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>


推荐答案

Apache XML上的.apache.org / Java / api / org / apache / xml / security / c14n / Canonicalizer.html> Canonicalizer 课程安全项目:

The Canonicalizer class at Apache XML Security project:

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

这篇关于Java:XML规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆