Java XStream - 如何忽略某些元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11052423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java XStream - How to ignore some elements
提问by Rui Lima
I have the following XML:
我有以下 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>
I just want to map the elements (node) to an object, but I'm having to problems:
我只想将元素(节点)映射到一个对象,但我遇到了问题:
- It's complaining about bounds elements, because I don't want to map them.
- Not all
nodes
havetags
so I'm getting some issues with it.
- 它抱怨边界元素,因为我不想映射它们。
- 并非所有人
nodes
都有,tags
所以我遇到了一些问题。
回答by Viktor Stolbin
Unfortunately overriding Mapper behaviour mentioned here does not work with implicit collections or annotations. I checked with version 1.4.3. So the obvious solution I found was to mock ignored fields with ommiting annotation. Works perfect for me but a bit boring to create them every time.
不幸的是,这里提到的覆盖 Mapper 行为不适用于隐式集合或注释。我检查了 1.4.3 版。所以我发现的明显解决方案是使用省略注释来模拟被忽略的字段。非常适合我,但每次创建它们都有些无聊。
@XStreamOmitField
private Object ignoredElement;
回答by krzysiek.ste
Since XStream 1.4.5 durring marshaller declaration it's enough to use ignoreEnknownElements() method:
由于 XStream 1.4.5 在 marshaller 声明期间使用 ignoreEnknownElements() 方法就足够了:
XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().ignoreUnknownElements();
...
to ignore unnecessary elements.
忽略不必要的元素。
回答by tk_
Simply define below anonymous class after Xtream deceleration.
只需在 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;
}
}
};
}
};
回答by Konstantin Pribluda
Plain googling reveals a lot of ioptions. This is one of them:
简单的谷歌搜索显示了很多选项。这是其中之一:
http://rafaelsteil.com/omit-unexpected-xml-elements-with-xstream/
http://rafaelsteil.com/omit-unexpected-xml-elements-with-xstream/