Announcement Announcement Module
Collapse
No announcement yet.
Spring Batch and XML Processing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Batch and XML Processing

    Hello,

    I have configured a StaxEventItemReader to read correctly the following XML file as input:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Export>
        <Header>
            <Material>Material</Material>
            <Address>Address</Address>
        </Header>
        <Body>        
            <Item>
                <RefDate>RefDate1</RefDate>
                <NameSurname>Name1</NameSurname>
                <Value>80</Value>
             </Item>
            <Item>
                <RefDate>RefDate2</RefDate>
                <NameSurname>Name2</NameSurname>
                <Value>42.9</Value>
            </Item>
            <Item>
                <RefDate>RefDate3</RefDate>
                <NameSurname>Name3</NameSurname>
                <Value>87.2</Value>
            </Item> 
        </Body>
    </Export>
    My problem is that I need to have a Fragment like this in order to store each item in my DB:

    - Material
    - Address
    - RefDate
    - NameSurname
    - Value

    But the "Material" and "Address" node are in the "Header" node and not directly in the "Item".

    How can I manage this composite fragment?
    Which node ROOT should I use as fragmentRootElementName property of the StaxEventItemReader? Is it "Export" node or "Item" node?

    Code:
        <bean id="itemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
            <property name="fragmentRootElementName" value="???" />
            <property name="resource" value="data/iosample/input/input.xml" />
            <property name="unmarshaller" ref="myMarshaller" />
        </bean>
    THANK YOU!
    Last edited by andrew007; Jul 12th, 2009, 07:25 AM.

  • #2
    If you need to inject the Header info into each Item, you can wrap the StaxEventItemReader in a custom ItemReader. Then, when you read the header, you can store the relevant information locally as a property on the reader. Later, as you read each individual item, you can inject that information into each one.

    Be sure to save that data to the Step ExecutionContext (via ItemStream) if you want the step to be restartable.

    Comment


    • #3
      spring batch and xml processing

      fragmentRootElementName="Export"

      but name Item as java.util.List
      Item must be list

      for example

      <bean id="contactMarshaller" class="org.springframework.oxm.xstream.XStreamMars haller">
      <property name="aliases">
      <util:map id="aliases">
      <entry key="order" value="com.batch.todb.Order"></entry>
      <entry key="product" value="java.lang.Long"></entry>
      <entry key="quantity" value="java.lang.Integer"></entry>
      <entry key="price" value="java.lang.Double"></entry>
      <entry key="orderitem" value="com.batch.todb.OrderItem"></entry>

      </util:map>
      </property>
      </bean>

      xml file is

      <order>
      <header date="Thu Apr 14 15:59:28 IST 2011" customer="Anand"></header>
      <orderitems>
      <orderitem>
      <product>111</product>
      <quantity>2</quantity>
      <price>8.90</price>
      </orderitem>
      <orderitem>
      <product>222</product>
      <quantity>7</quantity>
      <price>5.20</price>
      </orderitem>
      <orderitem>
      <product>333</product>
      <quantity>8</quantity>
      <price>6.30</price>
      </orderitem>
      </orderitems>
      </order>

      Comment

      Working...
      X