Results 1 to 4 of 4

Thread: StaxEventItemWriter of Spring Batch

  1. #1
    Join Date
    Sep 2012
    Posts
    8

    Default StaxEventItemWriter of Spring Batch

    Hi,

    My spring batch admin application generates XML as output via itemwriter. The problem is that if any tag in the generated in XML is empty, it gets discarded in the xml implicitly. Any help on the same.
    My expectation is if any tag is empty it should appear as <tagname></tagname> in the XML.

    Brief code snippet :

    StaxEventItemWriter<TransactionRecord> staxItemWriter = new StaxEventItemWriter<TransactionRecord>();
    Map<String, String> aliases = new HashMap<String, String>();
    aliases.put("TransactionRecord","com.thomascook.bb mm.bean.TransactionRecord");
    aliases.put("segmentId","java.lang.String");
    aliases.put("recordUse","java.lang.String");
    Marshaller marshaller = new XStreamMarshaller();
    ((XStreamMarshaller) marshaller).setAliases(aliases);
    staxItemWriter.setResource(resource);
    staxItemWriter.setMarshaller(marshaller);

    Please let me know if more information is required.

  2. #2
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    346

    Default

    Empty or null? I believe (but have not tested) that a null attribute will be dropped and an empty attribute (empty string for example) will generate an empty tag with the XStreamMarshaller. I'd have to write a test to confirm though...
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

  3. #3
    Join Date
    Sep 2012
    Posts
    8

    Default spring batch and staxeventwriter

    Quote Originally Posted by mminella View Post
    Empty or null? I believe (but have not tested) that a null attribute will be dropped and an empty attribute (empty string for example) will generate an empty tag with the XStreamMarshaller. I'd have to write a test to confirm though...
    Thanks for the input. But i want that both of the cases [empty and null values] to be included in the XML. I am passing a java object to be marshalled. Do i need to override any method of XStreamMarshaller in order to meet this requirement. For eg :
    <Transaction>
    <A>1</A>
    <B>xyz</B>
    <C></C>
    </Transaction>
    <Transaction>
    <A></A>
    <B></B>
    <C>123</C>
    </Transaction>

    Irrespective of the datatypes of child nodes, either it is empty string/null or any other dataypes with no value, I want that tag/node(with null or empty value) also to appear which currently is not happening. I hope I am able to express the problem.
    Last edited by ankitgupta; Dec 9th, 2012 at 11:20 PM. Reason: Typing mistake

  4. #4
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    346

    Default

    After doing some quick looking at the XStream documentation, null attributes are left out. There is a NullConverter you can configure to add but I'm not sure that is going to give you the output you want. I'd expect that you'll need to write a custom converter to handle this: http://xstream.codehaus.org/converter-tutorial.html.

    I haven't asked this up to now, but probably should. If you're using XML to serialize objects, why do you need the empty tags to exist? When the XML is read back in, any attributes that were not serialized in the first place will be empty again anyways...
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •