简体   繁体   English

如何规范化Stax XML对象

[英]How to Canonicalize a Stax XML object

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. 我想规范化Stax对象,该程序正在使用DOM进行处理,但是dom无法管理大型XML文档(例如1GB),因此STAX是解决方案。

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... 这个想法是用Stax执行下面的代码...

Thx :) 谢谢 :)

I solve this problem with XOM library, here is the equivalent code. 我用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));

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. 这段代码可以管理200Mb的文件,并且花7分钟时间进行规范化,如果您有疑问,请参阅XOM文档,它很清楚,并且有很多示例。

Thx to all for your comments :) 谢谢大家的意见:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM