Announcement Announcement Module
Collapse
No announcement yet.
Complex xml writing with chunk process. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Complex xml writing with chunk process.

    Hi.
    I have been working on one batch job and i have te generate xml file like:

    Code:
    <Transaction>
      <MerchantID>string</MerchantID>
      <UserID>string</UserID>
      <TransactionReferenceNumber>string</TransactionReferenceNumber>
      <TransactionType>string</TransactionType>
      <Returns>
        <Return>
          <RMAID>string</RMAID>
          <Remove>string</Remove>
          <ExpectedDate>string</ExpectedDate>
           <Line>
                 <LineNum>string</LineNum>
                  <ProductID>string</ProductID>
            </Line>
       </return>
    <Return>
          <RMAID>string</RMAID>
          <Remove>string</Remove>
          <ExpectedDate>string</ExpectedDate>
           <Line>
                 <LineNum>string</LineNum>
                  <ProductID>string</ProductID>
            </Line>
       </Return>
    ....
    </Returns>
    </Transaction>
    each process processes one Return object and the end i have StaxEventItemWriter<Return> which writes list of returns to xml file and I am getting:
    Code:
    <Transaction>
      <Returns>
        <Return>
          <RMAID>string</RMAID>
          <Remove>string</Remove>
          <ExpectedDate>string</ExpectedDate>
           <Line>
                 <LineNum>string</LineNum>
                  <ProductID>string</ProductID>
            </Line>
       </return>
    <Return>
          <RMAID>string</RMAID>
          <Remove>string</Remove>
          <ExpectedDate>string</ExpectedDate>
           <Line>
                 <LineNum>string</LineNum>
                  <ProductID>string</ProductID>
            </Line>
       </Return>
    ....
    </Returns>
    </Transaction>
    But root element does not have
    Code:
      <MerchantID>string</MerchantID>
      <UserID>string</UserID>
      <TransactionReferenceNumber>string</TransactionReferenceNumber>
      <TransactionType>string</TransactionType>
    tags, How can I add this to root element, I was using xstream marshaller, but jaxb2 is fine.

    I uses spring batch 2.x version
    Anyone knows how to generate this kind of format(adding some other elements to root)?
    Thanks
    Last edited by elbek; Mar 15th, 2013, 03:42 PM. Reason: corrected title

  • #2
    Can you please post your configuration?

    Comment


    • #3
      It is not an issue, It is something I do not know what to do and how to generate, I found a way to do so by manually, not using StaxEventItemWriter, Just using Jaxb2Marshaller and marshall by myself. It worked well.

      Now code looks like this:
      Code:
      public class MyWriter implements ItemWriter<Return> {
      
          @Autowired
          private LogFileWriterHelper logFileWriterHelper;
          private String rootTagName;
          private Jaxb2Marshaller marshaller;
      
          @Override
          public void write(List<? extends Item> items) throws Exception {
                      Transaction transaction = new Transaction();
                      transaction.setMerchantID(getMerchantID());
                      transaction.setPassword(getPassword());
                      transaction.setUserID(getUserID());
                      transaction.setTransactionType(TRANSACTION_TYPE);
                      transaction.setReturns(items);
                      FileOutputStream os = new FileOutputStream(getFile());
                      marshaller.marshal(transaction, new StreamResult(os));
          }
      }
      Thank you for your response.

      Comment


      • #4
        I'd still be interested in seeing your configuration. The XML you require should not be an issue to generate with the off the shelf components. Also, keep in mind the writer you posted above does not handle things like retry or restart.

        Comment


        • #5
          I do not think you generate the required one with using StaxEventItemWriter. I doubt there is way to do.
          Anyway here is my config:
          Code:
          <bean id="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
                  <property name="aliases">
                      <props>
                          <prop key="line">pojo.Line</prop>
                          <prop key="Return">pojo.Return</prop>
                      </props>
                  </property>
                  <property name="implicitCollections">
                      <props>
                          <prop key="pojo.Return">lines</prop>
                      </props>
                  </property>
                  <property name="converters">
                      <list>
                          <ref local="dateConverter"/>
                      </list>
                  </property>
                  <property name="omittedFields">
                      <props>
                          <prop key="pojo.Return">returnOrderActivity</prop>
                      </props>
                  </property>
              </bean>
          
          <bean class="path.to.StaxEventItemWriter">
                  <property name="marshaller" ref="marshaller"/>
                  <property name="rootTagName" value="Transaction"/>
          </bean>
          processor is:

          Code:
          public class Processor extends AProcessor<A, Return> {
          
              @Override
              public Return process(Aitem) throws Exception {
                    //convert to Return
              }
          
          }
          writer is StaxEventItemWriter

          Comment


          • #6
            Ahhh...what is the type that is being sent to the StaxEventItemWriter? To generate the output you require, I would expect the item to be of type Transaction with a collection of Return objects in it (along with the other attributes required) instead of the just the Return you are returning from the ItemProcessor. If you structure the job that way, it should work with the default configurations.
            Last edited by mminella; Mar 19th, 2013, 11:16 AM. Reason: clarified

            Comment


            • #7
              Originally posted by mminella View Post
              Ahhh...what is the type that is being sent to the StaxEventItemWriter? To generate the output you require, I would expect the item to be of type Transaction with a collection of Return objects in it (along with the other attributes required) instead of the just the Return you are returning from the ItemProcessor. If you structure the job that way, it should work with the default configurations.
              Sorry for incomplete answer.

              This is processor and returns Return type.
              Code:
              public class Processor extends AProcessor<A, Return> {
              
                  @Override
                  public Return process(Aitem) throws Exception {
                        //convert to Return
                  }
              
              }
              So to the StaxEventItemWriter class I will send List<? extends Return> same as MyWriter class.

              Comment


              • #8
                And that is why you are getting the XML you are. In order for you to get the XML you want, you need your processor to return Transaction and your writer to take Transaction.

                Comment


                • #9
                  "To generate the output you require, I would expect the item to be of type Transaction with a collection of Return objects in it"
                  That is exactly right, BUT batch job does not allow this, because I am processing Return type in one time, so batch job will call StaxEventItemWriter with List<? extends Return> param type.
                  If I want to use Transaction type for StaxEventItemWriter, then My processor should return Transaction type, But I have only one transaction object. This is the exact the problem. Sorry man for not giving the answers that you are looking for.

                  Comment


                  • #10
                    In other hand i can not put the Transaction type to processor.

                    Comment


                    • #11
                      You should be able to add these elements with a StaxWriterCallback header callback:

                      http://static.springsource.org/sprin...terCallback%29

                      Comment

                      Working...
                      X