Raj
Raj

Reputation: 2920

Memory-efficient XML manipulation in Java

We are in the process of implementing a transactional system that has two backend components:

The resulting XML is sent back to the requestor. Since we are likely doing this under heavy load, I'd like to do this in a very CPU/memory efficient way.

What is the best way to perform the above while keeping a tight leash on overall memory utilization?

Specifically, is my best best to do a DOM parse, of the output of Component A and pass that to Component B to modify in memory? Is there a better way to do this using SAX which may be more memory efficient? Are there standard libraries that do this via SAX or DOM?

Thanks for any insights.

-Raj

Upvotes: 1

Views: 603

Answers (3)

duffymo
duffymo

Reputation: 308998

An aspect or filter on componet B that applys an XSL-T transformation to the initial XML response might be a clean way to accomplish it. The memory utilization depends on the size of the request and the number of instances in memory. CPU will be dependent on these two factors as well.

DOM requires that the whole XML document be resident in memory before you modify it. If it's just a couple of elements that have to change, then SAX is a good alternative.

Upvotes: 1

Chris Dargis
Chris Dargis

Reputation: 6053

SAX is an event-based parsing utility. You are notified of events such as beginDocument(), startElement(), endElement(), etc. You save in memory the things you wish to save. You only control the events you want, which can really increase the speed of parsing and decrease the use of memory. It can be memory efficient, depending on what and how much of the things you are saving in memory. For the general case, SAX is more memory efficient versus DOM. DOM will save the entire document in memory in order to process it.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727017

Generally, SAX is more memory-efficient than DOM, because the entire document does not need to be loaded into memory for processing. The answer, however, depends on the specifics of your "Component B modifies the initial response XML" requirements.

  • If each change is local to its own XML sub-tree (i.e. you may need data from all nodes leading to the root of the tree, but not siblings), SAX will work better.
  • If the changes require referencing siblings to produce the results, DOM will work better, because it would let you avoid constructing your own data structure for storing the siblings.

Upvotes: 3

Related Questions