Java XStream - How to ignore some elements
Asked Answered
T

5

10

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 an object, but I'm having to problems:

  1. It's complaining about bounds elements, because I don't want to map them.
  2. Not all nodes have tags so I'm getting some issues with it.
Truculent answered 15/6, 2012 at 14:16 Comment(1)
Well, please show your xml and maybe source code where you are setting up xstreamBelter
M
14

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.

@XStreamOmitField
private Object ignoredElement;
Milliard answered 19/9, 2012 at 9:58 Comment(1)
Or, alternatively, you can also do: xStream.omitField(ClassName.class,"fieldName");Fold
C
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.

Costive answered 17/7, 2014 at 12:0 Comment(0)
L
1

Simply define below anonymous class after Xtream deceleration.

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;
                        }
                    }
                };
            }
        };
Lamoreaux answered 20/4, 2017 at 3:49 Comment(0)
M
0

you can do that by overriding the default marshaller.

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);
                    }
                };
        }
    };
}}
Manoeuvre answered 20/11, 2020 at 12:17 Comment(0)
B
-1

Plain googling reveals a lot of ioptions. This is one of them:

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

Belter answered 15/6, 2012 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.