Results 1 to 4 of 4

Thread: Getting double underscores in xml when using StaxEventItemWriter

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    Default Getting double underscores in xml when using StaxEventItemWriter

    Hi all,

    I'm new to spring batch and trying to use the StaxEventItemReader/Writer using XstreamMarshaller. The job runs ok but some of the xml tags/variables contains underscore, i.e job_ident, and they all come out with double underscores in the output file(job__ident). I think this is caused by Xstream using _ as escape character by default, thus resulting in the double underscores.

    Is there a way to configure StaxEventItemWriter or the XstreamMarshaller so that Xstream will use another escape character?

    Thanks,
    GON

  2. #2
    Join Date
    Jun 2005
    Posts
    4,241

    Default

    That's not really anything to do with Batch - we just delegate to the OXM marshaller. Try the Spring OXM (http://static.springsource.org/sprin.../html/oxm.html) and XStream documentation for more help.

  3. #3
    Join Date
    Dec 2009
    Posts
    3

    Default

    Hi

    In case others will experience the same problem some time in the future:

    I solved this by extending the XstreamMarshaller and overriding the method
    Code:
    protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException
      {
        SaxWriter saxWriter = new SaxWriter();
        saxWriter.setContentHandler(contentHandler);
        marshal(graph, saxWriter);
      }
    with
    Code:
    @Override
    	protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException {
    		SaxWriter saxWriter = new SaxWriter(new XmlFriendlyReplacer("ddd","_"));
    	    saxWriter.setContentHandler(contentHandler);
    	    marshal(graph, saxWriter);
    	}
    Don't know if this is the best solution, but it seems to work ok for me so far.

  4. #4
    Join Date
    Jul 2005
    Posts
    156

    Default

    Hello,

    I've just had the same problem today with Spring Batch.
    I know XStreamMashaller class is defined by Spring Framework, but since this problem happens mostly with Spring Batch usage (I mostly use XStreamMashaller in my batch applications), I wanted to have a Spring Batch user or dev point of view.

    I find XStreamMashaller configuration a bit tricky whenever I need to handle '_' escaping.

    Is it possible to simplify this and to have the same behaviour for every xml source / result type (stream / sax / dom / stax) ?

    For stream source / result type, we just need to configure streamDriver in XStreamMashaller.
    For Sax, Stax, DOM, we need to override one of the following methods :

    • marshalSaxHandlers
    • marshalDomNode
    • ou equivalent unmarshal


    Marshalling Stream Results
    For instance, when using the following code to unmarshal an object :
    Code:
    StringResult lResult = new StringResult();
    DocFileNet lDocument = new DocFileNet();
    lDocument.setCanalOperateur("canal1");
    marshaller.marshal(lDocument, lResult);
    Customizing XStreamMarshaller is sufficient to handle _ escaping.
    So I have :
    Code:
    <bean id="indexFileNetMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="streamDriver">
              <bean class="com.thoughtworks.xstream.io.xml.XppDriver">
                  <constructor-arg>
                      <bean class="com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer">
                          <constructor-arg index="0" value="_-"/>
                          <constructor-arg index="1" value="_"/>
                      </bean>
                  </constructor-arg>
              </bean>
          </property>
          <property name="annotatedClasses">
              <list>
                  <value>com.xxx.demat.filenet.entite.DocFileNet</value>
              </list>
          </property>
    </bean>

    Marshalling With Spring Batch StaxEventItemWriter

    I need to override marshalSaxHandlers method in XStreamMarshaller as noted in this thread by GONP.

    Thanks very much for your input.

Posting Permissions

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