How can I make a XStreamMarshaller skip unknown binding?

ぐ巨炮叔叔 提交于 2020-02-25 09:16:07

问题


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.


回答1:


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?
    }
}


来源:https://stackoverflow.com/questions/28478165/how-can-i-make-a-xstreammarshaller-skip-unknown-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!