Spring batch: read xml as DOM Document

I want to read a large xml file which includes n number of elements ‘elem1’. n can be up to 10.000 or more. I want to read each eleme1 as a DOM document, process it and read next elem1 etc. All articles I found so far suggest using StaxEventItemReader. But I don’t want to map the peeked ‘elem1’ to an object but a DOM document. The reason is, that the business logic which I want to integrate in proccessor allready exists. The logic handles with a DOM Document(reading Nodes etc..). However StaxEventItemReader expects a class type to map the read data to. Is there any way to read data just as a dom document? I was thinking of a multiline records reader and defining open/close tags as tockenizers. But I don’t know if this is possible and whether it is the right way?

 <root>     <elem1>         <a>...</a>         <b>...</b>     </elem1>       <elem1>         <a>...</a>         <b>...</b>         <c>...</c>     </elem1>   </root> 
Add Comment
1 Answer(s)

Since you are able to parse a string element into a DOM object, you can proceed as follows:

  • Use a StaxEventItemReader<String> to read string elements
  • Use a processor to transform string items into DOM items
  • Add another processor (in a composite processor) to process DOM items as needed (call the business logic that you already have and which expects DOM items)
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.