我已经实现了一般的解组:
public XMLProcessor(XMLFile file) throws JAXBException, IOException, SAXException {
JAXBContext jc = JAXBContext.newInstance(Customers.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File(file.getFile().getOriginalFilename());
file.getFile().transferTo(xml);
this.customers = (Customers) unmarshaller.unmarshal(xml);
}它工作得很好,但是处理一百万个客户的XML花费了一分钟多的时间。
我可以通过创建多个线程并同时解组XML文件的几个部分来提高性能吗?
你能给我看一些我的案例的示例代码吗?
发布于 2015-07-20 14:21:26
虽然我还不能提供一个完整的解决方案,但我想与您分享我目前正在实现的类似问题的方法。我的XML文件结构如下:
<products>
<product id ="p1">
<variant id="v1"></variant>
<variant id="v2"></variant>
</product>
<product id ="p2">
<variant id="v3"></variant>
<variant id="v4"></variant>
</product>
</products>产品和变体可能相当复杂,有很多属性、列表等。
我目前的方法是使用SAX来提取单个产品实体的XML流,然后将其移交给新的Unmarshaller Thread (使用标准多线程操作,限制为最大线程数等)。
但是,我仍然不能百分之百确定SAX是否会产生过多的开销(这可能会消耗掉多线程带来的好处)。如果是这种情况,我将尝试直接读取XML-stream,对"“的开始/结束标记做出反应。A这不是符合xml的,这是我最后的手段
https://stackoverflow.com/questions/30139302
复制相似问题