如何规范 Stax XML 对象

发布于 2024-08-31 09:11:23 字数 609 浏览 2 评论 0原文

我想规范化一个 Stax 对象,该程序使用 DOM 来实现,但 dom 无法管理大型 XML 文档(如 1GB),所以 STAX 是解决方案。

我的代码是:

File file=new File("big-1gb.xml");

org.apache.xml.security.Init.init(); 
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder documentBuilder = dfactory.newDocumentBuilder();

Document doc = documentBuilder.parse(file);

Canonicalizer c14n = Canonicalizer.getInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");

outputBytes = c14n.canonicalizeSubtree(doc.getElementsByTagName("SomeTag").item(0));

它的想法是用 Stax 执行下面的代码...

Thx :)

i want to Canonicalize a Stax object, the program it's doing it with DOM, but dom can't manage big XML documents (like 1GB), so STAX it's the solution.

The Code that i have it's:

File file=new File("big-1gb.xml");

org.apache.xml.security.Init.init(); 
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder documentBuilder = dfactory.newDocumentBuilder();

Document doc = documentBuilder.parse(file);

Canonicalizer c14n = Canonicalizer.getInstance("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");

outputBytes = c14n.canonicalizeSubtree(doc.getElementsByTagName("SomeTag").item(0));

The idea it's do the code below with Stax...

Thx :)

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

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

发布评论

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

评论(1

飘落散花 2024-09-07 09:11:23

我用 XOM 库解决了这个问题,这里是等效的代码。

ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
ObjectOutputStream outputstream = new ObjectOutputStream(bytestream);

nu.xom.Builder builder = new nu.xom.Builder(false, new nu.xom.samples.MinimalNodeFactory()); //The false parameter is for avoid a ValidationException that trows XOM

try {
nu.xom.canonical.Canonicalizer outputter = new nu.xom.canonical.Canonicalizer(outputstream);
nu.xom.Document input = builder.build(file);
outputter.write(input);
 }
catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
}

outputstream.close();

MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
sha1.update(java.nio.ByteBuffer.wrap(bytestream.toByteArray()));
salidasha1=sha1.digest();
String tagDigestValue=new String(Base64.encodeBase64(salidasha1));

这段代码可以管理200Mb的文件,并且需要7分钟来进行规范化,如果您有疑问,请参阅XOM文档,它非常清晰并且有很多示例。

感谢大家的评论:)

I solve this problem with XOM library, here is the equivalent code.

ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
ObjectOutputStream outputstream = new ObjectOutputStream(bytestream);

nu.xom.Builder builder = new nu.xom.Builder(false, new nu.xom.samples.MinimalNodeFactory()); //The false parameter is for avoid a ValidationException that trows XOM

try {
nu.xom.canonical.Canonicalizer outputter = new nu.xom.canonical.Canonicalizer(outputstream);
nu.xom.Document input = builder.build(file);
outputter.write(input);
 }
catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
}

outputstream.close();

MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.reset();
sha1.update(java.nio.ByteBuffer.wrap(bytestream.toByteArray()));
salidasha1=sha1.digest();
String tagDigestValue=new String(Base64.encodeBase64(salidasha1));

This code can manage files of 200Mb, and take 7 minutes to do the canonicalization, if you have doubt's, see the XOM documentation, it's pretty clear and have a lot of Examples.

Thx to all for your comments :)

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