如何在Java中加载大型XML文件 [英] How to load a big XML file in Java

查看:135
本文介绍了如何在Java中加载大型XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载大小为20-40 MB的大型XML文件的最佳方法是什么?

我尝试过使用DOMParser和DocumentBuilder,但我得到的只是以下内容:错误:

java.lang.OutOfMemoryError:Java堆空间

What is the best way to load a big XML file that''s 20-40 MB in size?
I have tried using DOMParser and DocumentBuilder but, all I get is the following as an error:

java.lang.OutOfMemoryError: Java heap space

推荐答案

1。您真的需要将整个XML文档加载到DOM结构中吗?你想用它做什么?

如果你可以看看替代品,请参阅SAX或VTD,其他不需要完全加载到内存中的替代品。也许你需要的只是某种XMLStreamReader。


2.你可以增加你的java运行时分配的堆空间。
1. Do you really need the whole XML document to be loaded into a DOM structure? What are you trying to do with it?
If you can look at alternatives, see SAX, or VTD, other alternatives that don''t require full loading into memory. Perhaps all you need is some sort of XMLStreamReader.

2. You could increase the allocated heap space of your java runtime.


@jkmyoung


是的,我需要一直使用整个XML。
@jkmyoung
Yes I need the whole XML at a stretch.


我的意思是,一下子吗?

如果是这样,你如何运行你的java程序?


java -Xms<初始堆大小> -Xmx<最大堆大小>

例如:java -Xms32m -Xmx128m
I meant, all at once?
If so then how are you running your java program?

java -Xms<initial heap size> -Xmx<maximum heap size>
eg: java -Xms32m -Xmx128m


这篇关于如何在Java中加载大型XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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