How can I make a XStreamMarshaller skip unknown binding?
Asked Answered
G

1

1

I'm working on a Spring-Batch program. I unmarshalls XML files with XStreamMarshaller.

How can I make a XStreamMarshaller to skip any unknown+unannoated fields?

<bean id="merge.reader.item"
      class="org.springframework.batch.item.xml.StaxEventItemReader">
  <property name="fragmentRootElementName" value="xml-fragment"/>
  <property name="unmarshaller" ref="merge.reader.unmarshaller"/>
</bean>
<bean id="merge.reader.unmarshaller"
      class="org.springframework.oxm.xstream.XStreamMarshaller">
  <property name="aliases" ref="merge.reader.binder"/>
  <property name="autodetectAnnotations" value="true"/>
</bean>
<util:map id="merge.reader.binder">
  <entry key="xml-fragment" value="path.to.my.Model"/>
</util:map>
public class Model {

    @XStreamAlias(value = "one")
    private String one;

    @XStreamAlias(value = "other")
    private String other;
}

The problem is that some new xml elements will be introduced in some other time.

I don't want to (actually I can't) add extra fields to my Model.

Grandniece answered 12/2, 2015 at 13:1 Comment(1)
If you have control over the creation of the XStream instance, this is your answer: #4410217Anacoluthia
G
3

I'm answering for my own question. The solution is where @biziclop linked. (disclaimer: I also answered the same answer on that post).

public class ExtendedXStreamMarshaller extends XStreamMarshaller {

    @Override
    protected void configureXStream(final XStream xstream) {
        super.configureXStream(xstream);
        xstream.ignoreUnknownElements(); // will it blend?
    }
}
Grandniece answered 12/2, 2015 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.