Search code examples
javaxmlspring-batchxstream

Unmarshall single non-root node of a long XML file


I have an XML that looks like this:

<root>
  <header>
    ...
  </header>
  <entries>
    ...
  </entries>
</root>

There is a single header node, but a lot of entries. Is there a way to extract this node as a Java bean, without reading the whole XML file.

If possible, is there any support for this in SpringBatch or XStream?

I don't think using a SpringBatch chunk-oriented tasklet with a StaxEventItemReader is appropriate since I just want to read one item.


Solution

  • You can write your own ItemReader extending StaxEventItemReader (or, better, delegating it), redefine method ItemReader.read() and change the condition to indicate reader is "exhausted" just returning null after first valid node read: when Spring-batch found ItemReader.read() method returning null it will stop processing file.
    You can use chunk-oriented and avoid full file read.