Announcement Announcement Module
Collapse
No announcement yet.
Multiple StaxEventItemReader/FlatFileWriter from one file in one step ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple StaxEventItemReader/FlatFileWriter from one file in one step ?

    Hi,

    I'm a new Spring-Batch user and I need help to understand how to organize my job :
    I have one xml file with 3 main tags, each having a small small structure. I want to extract data from theses tags and write it to 3 csv files. For now I used 3 step (I read my xml 3 times) and it works well but is it possible to optimize the batch with only one reading ? I know the 3 tag names and so the 3 final filenames.

    Thanks for your advices and sorry for my english.
    Ben

  • #2
    When you say "3 main tags", do you mean you have three correctly defined XML documents in the same file? If so, I'd recommend splitting them first. Otherwise, can you post an example of how your file is formatted?

    Comment


    • #3
      An exemple with this xm file. In this exemple the result will be 3 csv files : Temperatures.csv, Precipitations.csv and Winds.csv. I think the best is to read the xml file once and to write data into csv files dependently to the xml fragment found by the StaxEventItemReader. But how to do this ? Of course my real xml file is big that's why I'm searching to optimise.


      HTML Code:
      <?xml version="1.0" encoding="utf-8"?>
      <RootTag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance>
      	<Temperatures>
      		<Temperature>
      			<Date>2013-03-12</Date>
      			<Max>20</Max>
      			<Min>10</Min>
      		</Temperature>
      		<Temperature>
      			<Date>2013-03-13</Date>
      			<Max>22</Max>
      			<Min>11</Min>
      		</Temperature>
      	</Temperatures>
      	<Precipitations>
      		<Precipitation>
      			<Date>2013-03-12</Date>
      			<Hour>1</Hour>
      			<Quantity>0</Quantity>
      		</Precipitation>
      		<Precipitation>
      			<Date>2013-03-12</Date>
      			<Hour>2</Hour>
      			<Quantity>0</Quantity>
      		</Precipitation>
      		<Precipitation>
      			<Date>2013-03-12</Date>
      			<Hour>3</Hour>
      			<Quantity>1.5</Quantity>
      		</Precipitation>
      	</Precipitations>
      	<Winds>
      		<Wind>
      			<Date>2013-03-12</Date>
      			<Hour>1</Hour>
      			<WindSpeed>15</WindSpeed>
      		</Wind>
      		<Wind>
      			<Date>2013-03-12</Date>
      			<Hour>2</Hour>
      			<WindSpeed>20</WindSpeed>
      		</Wind>
      	</Winds>
      </RootTag>

      Comment


      • #4
        Given the XML you have, I would configure the unmarshaller with three aliases, one for Temperature, one for Precipitation, and one for Wind. From there, I'd use a ClassifierCompositeItemWriter to delegate the writing of each type to the appropriate ItemWriter (one for each CSV file as your requirement specifies).

        Comment


        • #5
          Finally I use a ClassifierCompositeItemWriter and a BackToBackPatternClassifier and I also rewrite the StaxEventItemReader to accept more than one fragment root name. Now it works well !

          Thank you for your help.

          Comment

          Working...
          X