10

我有以下 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,所以我遇到了一些问题。
4

5 回答 5

13

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

@XStreamOmitField
private Object ignoredElement;
于 2012-09-19T09:58:15.940 回答
11

Since XStream 1.4.5 durring marshaller declaration it's enough to use ignoreEnknownElements() method:

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

to ignore unnecessary elements.

于 2014-07-17T12:00:26.757 回答
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;
                        }
                    }
                };
            }
        };
于 2017-04-20T03:49:20.207 回答
0

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

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

于 2012-06-15T14:20:06.977 回答
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);
                    }
                };
        }
    };
}}
于 2020-11-20T12:17:04.903 回答