简体   繁体   English

使用Out Of Memory Error Java编写大型XML文件

[英]Writing a large XML file with Out Of Memory Error Java

I am developing an app for android, I need to create a rather large XML file. 我正在开发一个Android应用程序,我需要创建一个相当大的XML文件。

I have tried this with a DOM document however because it resides in memory it throws an 'out of memory' error. 我已经尝试过使用DOM文档但是因为它驻留在内存中会引发“内存不足”错误。

Can someone suggest an alternative solution? 有人可以建议替代解决方案吗? This is a XML file create from scratch. 这是从头开始创建的XML文件。

I would write the XML to a Stream or Writer progressively as to traverse the data you want to turn into XML. 我会逐步将XML写入Stream或Writer,以遍历您想要转换为XML的数据。 This way you don't need much more memory than the original data. 这样,您不需要比原始数据更多的内存。

You will always go out of memory for Large Data size. 对于大数据大小,您将始终内存不足。 How about dividing your Data and creating small chuncks and then appending all those together. 如何划分数据并创建小块,然后将所有这些组合在一起。

Or an other option is using SAX parser which consumes less memory . 或者另一个选择是使用SAX解析器,它消耗更少的内存

Streaming XML is the way to go. 流式XML是要走的路。 The most 'vanilla' approach is probably using StAX: http://docs.oracle.com/javaee/5/tutorial/doc/bnbem.html#bnbff 最“香草”的方法可能是使用StAX: http//docs.oracle.com/javaee/5/tutorial/doc/bnbem.html#bnbff

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

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