Java XStream - how to ignore some elements

I have the following 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 the object, but I'm having problems:

  • He complains about the elements of boundaries, because I do not want to compare them.
  • Not all nodes have tags , so I get some problems with it.
+6
source share
4 answers

Unfortunately, overriding the Mapper behavior mentioned here does not work with implicit collections or annotations. I checked version 1.4.3. So the obvious solution I found was to mock ignored fields with ommiting annotation. Works perfect for me, but a little boring to create them every time.

 @XStreamOmitField private Object ignoredElement; 
+7
source

Since XStream 1.4.5 issues a marshaller declaration, it is sufficient to use the ignoreEnknownElements () method:

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

ignore unnecessary items.

+5
source

Just define below an anonymous class after braking 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; } } }; } }; 
+1
source

Conventional googling features many ioptions. This is one of them:

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

0
source

Source: https://habr.com/ru/post/918193/


All Articles