XStream UnknownFieldException: Skip unmapped tags while parsing XML

By default XStream requires the developer to declare a property for each possible XML tag. For example if the XML file contains the tag X but the Java class doesn't, the parser will generate and UnknownFieldException

com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field ...

To skip unmapped tags we can use the following code (it also works with implicit collections):

XStream xStream = new XStream() {
           @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @Override
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                     if (definedIn == Object.class) {
                     try {
                     return this.realClass(fieldName) != null;
                     } catch(Exception e) {
                     return false;
                     }
                     } else {
                            return super.shouldSerializeMember(definedIn, fieldName);
                        }
                    }
                };
            }
        };
tags: