Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a large (30MB+) xml file in java?

The file itself is not that big, should fit in memory. But once you combine that with other overhead factors then starts to become a problem. We are building a DOM in memory and that is not scaling for us. Using raw output streams seems problematic in the sense that we have to be careful about escaping characters.

What are some good approaches for doing this?

Are there any goods libs for this?

like image 221
Artilheiro Avatar asked Mar 16 '11 21:03

Artilheiro


2 Answers

STAX provides a convenient API with which to write XML to output stream. Easy tutorial here.

like image 124
sudocode Avatar answered Sep 18 '22 21:09

sudocode


Try XStream

Features

  • Ease of use. A high level facade is supplied that simplifies common use cases.
  • No mappings required. Most objects can be serialized without need for specifying mappings.
  • Performance. Speed and low memory footprint are a crucial part of the design, making it suitable for large object graphs or systems with high message throughput.
  • Clean XML. No information is duplicated that can be obtained via reflection. This results in XML that is easier to read for humans and more compact than native Java serialization.
  • Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.
  • Full object graph support. Duplicate references encountered in the object-model will be maintained. Supports circular references.
  • Integrates with other XML APIs. By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML).
  • Customizable conversion strategies. Strategies can be registered allowing customization of how particular types are represented as XML.
  • Error messages. When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem.
  • Alternative output format. The modular design allows other output formats. XStream ships currently with JSON support and morphing.
like image 36
adarshr Avatar answered Sep 21 '22 21:09

adarshr