Web / Apache Axiom Interview questions
What is the difference between serialize() and serializeAndConsume()?
Both methods write an OMElement's XML out to a destination, but they differ in what happens to the tree - and the underlying parser - afterward.
serialize(writer) ensures the tree gets fully built as a side effect of writing it, caching each node it touches; after the call returns, the element remains a normal, fully navigable, re-serializable OM tree, independent of the original input stream.
serializeAndConsume(writer) instead streams events straight from the underlying parser without retaining the built nodes in memory - it's generally faster and lighter for a genuine one-shot write, but it leaves the tree "consumed": attempting to navigate or serialize it again afterward is unsupported and typically throws an exception, since the underlying stream has already been drained.
| serialize() | serializeAndConsume() |
| Caches nodes; tree remains usable after | Streams and discards; tree unusable after |
| Slightly more memory retained | Lower memory footprint for one-shot output |
| Use when you'll read/serialize again | Use for genuine single-pass output |
More Related questions...