I have serializable relational javabean BO's in an ArrayList (boList) that I want to write to XML using XMLEncoder or some other standard method.
Using:
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("C:/tmp/test.xml")));
encoder.writeObject(boList);
encoder.close();
I run out of memory. If, however, I do this:
for(BO bo : boList)
{
encoder.writeObject(bo);
encoder.flush();
}
encoder.close();
It runs perfectly using very little memory. I know the flush() writes generated xml to the file and releases referenced memory which makes sense. I suspect when writing the boList the XMLEncoder it is trying to generate all XML before writing for which there is not enough memory.
I need to be able to write the boList to XML as it will be a field in a parent BO which will be written to XML. How do I get this to behave with the list as in the case where I call "writeObject" on each separate object? Is there an alternative to XMLEncoder that will accomplish this which is as straightforward to use? This must be accomplished using standard JRE/Java 6 functionality with no third party libraries. Thanks!
After a great deal of research, and reading of implementation code for the XMLEncoder, I have determined that it does indeed build all of the xml structure in memory before writing the output for each call to writeObject(...). There does not seem to be a feasible way around this using XMLEncoder.
I have decided to use an alternative third-party opensource library. XStream (http://x-stream.github.io/) provides exactly the same end functionality as XMLEncoder in this case, but is far more robust. It streams the generated xml to the destination as it is being generated resulting in a much reduced memory footprint.
In short, my answer is that XMLEncoder's memory footprint can not be reduced for large object graphs without actually changing XMLEncoder's implementation.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments