Java XStream - 如何忽略某些元素

时间:2012-06-15 14:16:41

标签: java xstream

我有以下XML:

<xml version="1.0" encoding="UTF-8"?> 
<osm version="0.6" generator="CGImap 0.0.2">
 <bounds minlat="48.1400000" minlon="11.5400000" maxlat="48.1450000" maxlon="11.5430000"/>
 <node id="398692" lat="48.1452196" lon="11.5414971" user="Peter14" uid="13832" visible="true" version="18" changeset="10762013" timestamp="2012-02-22T18:59:41Z">
 </node>
 <node id="1956100" lat="48.1434822" lon="11.5487963" user="Peter14" uid="13832" visible="true" version="41" changeset="10762013" timestamp="2012-02-22T18:59:39Z">
  <tag k="crossing" v="traffic_signals"/>
  <tag k="highway" v="traffic_signals"/>
  <tag k="TMC:cid_58:tabcd_1:Class" v="Point"/>
  <tag k="TMC:cid_58:tabcd_1:Direction" v="positive"/>
  <tag k="TMC:cid_58:tabcd_1:LCLversion" v="9.00"/>
  <tag k="TMC:cid_58:tabcd_1:LocationCode" v="35356"/>
  <tag k="TMC:cid_58:tabcd_1:NextLocationCode" v="35357"/>
  <tag k="TMC:cid_58:tabcd_1:PrevLocationCode" v="35355"/>
 </node>
</osm>

我只是想将元素(节点)映射到一个对象,但我遇到了问题:

  1. 它抱怨边界元素,因为我不想映射它们。
  2. 并非所有nodes都有tags,所以我遇到了一些问题。

5 个答案:

答案 0 :(得分:12)

不幸的是,这里提到的覆盖Mapper行为不适用于隐式集合或注释。我查看了1.4.3版本。 所以我发现的显而易见的解决方案是通过省略注释来模拟忽略的字段。对我来说是完美的,但每次创建它们都有点无聊。

@XStreamOmitField
private Object ignoredElement;

答案 1 :(得分:9)

由于XStream 1.4.5在marshaller声明中声明它足以使用ignoreEnknownElements()方法:

XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().ignoreUnknownElements();
...

忽略不必要的元素。

答案 2 :(得分:1)

在Xtream减速后简单定义下面的匿名类。

XStream xstream = new XStream(new DomDriver()){  
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        try {
                            return definedIn != Object.class || realClass(fieldName) != null;
                        } catch(CannotResolveClassException cnrce) {
                            return false;
                        }
                    }
                };
            }
        };

答案 3 :(得分:0)

普通的谷歌搜索揭示了很多的ioptions。这是其中之一:

http://rafaelsteil.com/omit-unexpected-xml-elements-with-xstream/

答案 4 :(得分:0)

您可以通过覆盖默认的编组器来做到这一点。

import org.springframework.oxm.xstream.XStreamMarshaller;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.MapperWrapper;

public class CustomXStreamMarshaller extends XStreamMarshaller {

@Override
protected XStream constructXStream() {
    // The referenced XStream constructor has been deprecated as of 1.4.5.
    // We're preserving this call for broader XStream 1.4.x compatibility.
    return new XStream() {
        @Override
        protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @Override
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                        return super.shouldSerializeMember(definedIn, fieldName);
                    }
                };
        }
    };
}}