Announcement Announcement Module
Collapse
No announcement yet.
Customising StaxEventItemWriter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Customising StaxEventItemWriter

    I am currently using a batch process that read records from the database and writes out XML using a StaxEventItemWriter. I posted previously about a problem I had with the root tag where I need to generate something like the following:

    Code:
    <DocumentRoot>
    <RootChild>
         ---- all my dynamic writes from writer.write() go in here
    </RootChild>
    </Document>
    I did not know how to customise the StaxEventItemWriter to have two tags (DocumentRoot and RootChild) written as the 'root' so I decided to write my own class (which is just a copy paste of the StaxEventItemWriter) and customise the startDocument and endDocument of the writer to add the following:

    In startDocument(XMLEventWriter writer):

    Code:
    ...
    if (!rootChildTagName.isEmpty())
    {
           writer.add(factory.createStartElement(getRootTagNamespacePrefix(),
    	getRootTagNamespace(), rootChildTagName));
    }
    ...
    In endDocument(XMLEventWriter writer) :

    Code:
    ...
    if (!rootChildTagName.isEmpty())
    {
    	bufferedWriter.write("</" + nsPrefix + getRootChildTagName() + ">");
    }
    ...
    I feel uncomfortable rewriting the StaxEventItemWriter, I would have thought being able to extend it to override startDocument() and endDocument() would be a better solution. However if I try to extend then I do not have access to the bufferedWriter in endDocument() and from the comments I believe that this bufferedWriter is crucial for maintaining consistency during restarts. Is there a better solution than re-writing the StaxEventItemWriter or does the next version of this class need to allow you to over-ride the endDocument properly?

  • #2
    Have you considered using a header/footer callback for the inner root tags?

    Comment


    • #3
      Sometimes I can't see the wood for the trees Thanks that works perfectly and is so much simpler!

      Comment


      • #4
        I think you will have problems with restart if you are using a footer callback to write the </RootChild> (same reason why in StaxEventItemWriter the root tag is closed with the BufferedWriter instead of XMLEventWriter).

        Comment


        • #5
          Yes that was one of the things I was originally concerned about as I saw the comments in the Spring code. So is the only alternative to write my own version of the StaxEventItemWriter?

          Comment

          Working...
          X