Java XStream - Ignore tag that doesn't exist in XML
Asked Answered
P

6

12

I currently use a piece of XML like the following

<Person>
    <Name>Frank Smith</Name>
    <Id>100023412</Id>
    <DOB>12/05/1954</DOB>
    <LasLogin>01/09/2010</LasLogin>
    <FavOS>Windows</FavOS>      // Wild card that may occasionally appear
</Person>

What I am stuck with, is when using XStream I need to be able to ignore certain tags that appear (in the case above 'FavOS') These tags may not be known or change in the future. Is there a way to Ignore all tags that do not match what is currently implemented?

(Using XStream 1.3.1)

Pail answered 10/12, 2010 at 14:24 Comment(0)
B
18

As it took me more than 15 minutes to find this answer, I thought I would post it.

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;
                        }
                    }
                };
            }
        };

This seems to skip xml items that are not in your objects.

Bittner answered 4/1, 2011 at 14:26 Comment(1)
Does this work if you dont know the name of the field you want to ignoreCraze
C
6

XStream 1.4.5 supports dealing with tags which are not implemented. Use ignoreUnknownElements for tags which are not implemented yet or has been removed and you are dealing with old xml. You can also specify which particular tag you would like to ignore.

Carbolated answered 15/10, 2013 at 17:20 Comment(0)
B
4

First of all, thanks for sharing this answer. It was very useful. However, the code mentioned above has issues. It does not have @Override annotations, which are a must to use this piece of code. Here is the updated code that works:

    XStream xstream = new XStream(new StaxDriver()) {
          @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);
              }
            };
          }
        };
Bartolommeo answered 28/9, 2012 at 18:22 Comment(0)
E
0

From the x-stream FAQ:

How does XStream deal with newer versions of classes?

  • If a new field is added to the class, deserializing an old version will leave the field uninitialized.
  • If a field is removed from the class, deserializing an old version that contains the field will cause an exception. Leaving the field in place but declaring it as transient will avoid the exception, but XStream will not try to deserialize it.
  • ...
  • implement a custom mapper to ignore unknown fields automatically (see acceptance test CustomMapperTest.testCanBeUsedToOmitUnexpectedElements())
Ec answered 10/12, 2010 at 15:6 Comment(1)
cheers, I did have a hunt around and seems to be an issue since 2004.Pail
C
0

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.

Clump answered 18/7, 2014 at 6:48 Comment(0)
W
0

I asked for exactly the same problem.

How can I make a XStreamMarshaller skip unknown binding?

And I got a comment linking this post.

I solved the my problem by extending the XStreamMarshaller.

public class ExtendedXStreamMarshaller extends XStreamMarshaller {

    @Override
    protected void configureXStream(final XStream xstream) {
        super.configureXStream(xstream);
        xstream.ignoreUnknownElements(); // will it blend?
    }
}
Waterresistant answered 12/2, 2015 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.