让JAXB更快 [英] Make JAXB go faster

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

问题描述

我有一个8兆的文件。使用JAXB进行编组需要1082ms,使用DOM需要862ms,使用SAX需要438ms。这是使用JDK 1.6的所有默认值,没有使用额外的配置,例如使用woodstox。

I have a 8 Meg file. Marshalling using JAXB takes 1082ms, using DOM takes 862ms, using SAX takes 438ms. This is using all defaults with JDK 1.6, no extra configuration such as using woodstox is used.

为了从JAXB获得更好的性能,我尝试制作它通过执行SAX解析...

In an effort, to get better performance from JAXB, I try to make it use SAX parsing by doing...

FileReader fr = new FileReader("myfile.xml");
JAXBContext jc = JAXBContext.newInstance(MyObjectList.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();

XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLEventReader xmler = xmlif.createXMLEventReader(fr);

long beginTime = System.currentTimeMillis();
MyObjectList obj = (MyObjectList)unmarshaller.unmarshal(xmler);
long endTime = System.currentTimeMillis();

这使得它变得更慢 - 3207ms。

This makes it go even slower - 3207ms.

我的问题是:
1.如何让JAXB更快?
2.我怎么能100%确定它使用的是什么底层解析机制?

My questions are: 1. How do I make JAXB go faster? 2. How can I be 100% sure what underlying parsing mechanism it is using?

推荐答案


1 - 如何让JAXB变得更快?

1 - How do I make JAXB go faster?

你在StAX输入的解组中走在正确的轨道上,但是我建议使用XMLStreamReader而不是XMLEventReader。

You are on the right track with unmarshalling from a StAX input, but I would recommend a XMLStreamReader instead of a XMLEventReader.

XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLStreamReader xmler = xmlif.createXMLStreamReader(fr);

由于StAX是标准,您可以切换另一个实现,例如 WoodStox 作为底层解析器。

Since StAX is a standard you can switch in another implementation such as WoodStox as the underlying parser.


2 - 怎么能我100%确定它正在使用什么底层解析机制?

2 - How can I be 100% sure what underlying parsing mechanism it is using?

就像你正在做的那样。如果您传递一个JAXB实现 XMLStreamReader 的实例,那么您可以合理地确定它正在被使用。另一方面,如果你从 InputStream 之类的东西中解组,那么JAXB实现可以自由地使用它想要的任何解析技术。如果你选择Woodstox,请务必查看性能页面:

Just like you are doing. If you pass a JAXB implementation an instance of XMLStreamReader then you can be reasonably sure that it is being used. If on the other hand you unmarshalled from something like an InputStream then the JAXB implementation is free to use whatever parsing technique it wants to. If you go with Woodstox be sure to check out there performance page as well:

  • http://woodstox.codehaus.org/Performance

这篇关于让JAXB更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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