I currently use a piece of XML like the following
我目前使用的XML片段如下所示
<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?
我所坚持的是,在使用XStream时,我需要能够忽略出现的某些标记(在上面的“FavOS”中),这些标记将来可能不会被知道或更改。是否有一种方法可以忽略与当前实现的不匹配的所有标记?
(Using XStream 1.3.1)
(使用XStream 1.3.1)
6 个解决方案
#1
16
As it took me more than 15 minutes to find this answer, I thought I would post it.
我花了15分钟才找到这个答案,我想我应该把它贴出来。
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.
这似乎跳过了对象中没有的xml项。
#2
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.
XStream 1.4.5支持处理未实现的标记。使用ignoreUnknownElements对那些尚未实现或已被删除且正在处理旧xml的标记。您还可以指定要忽略的特定标记。
#3
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:
首先,谢谢你分享这个答案。这是非常有用的。但是,上面提到的代码有问题。它没有@Override注释,这是使用这段代码所必需的。以下是更新后的代码:
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);
}
};
}
};
#4
0
From the x-stream FAQ:
从x-stream常见问题解答:
How does XStream deal with newer versions of classes?
XStream如何处理新版本的类?
- 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.
- 如果从类中删除字段,反序列化包含该字段的旧版本将导致异常。将字段保留在适当的位置,但将其声明为瞬态将避免异常,但是XStream不会尝试反序列化它。
- ...
- …
- implement a custom mapper to ignore unknown fields automatically (see acceptance test CustomMapperTest.testCanBeUsedToOmitUnexpectedElements())
- 实现一个自定义映射器来自动忽略未知字段(参见验收测试custommappertest.testcanbeusedtoomitunexpected tedelements ()))
#5
0
Since XStream 1.4.5 durring marshaller declaration it's enough to use ignoreEnknownElements() method:
由于XStream 1.4.5 durring marshaller声明时使用了ignoreEnknownElements()方法就足够了:
XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().ignoreUnknownElements();
...
to ignore unnecessary elements.
忽略不必要的元素。
#6
0
I asked for exactly the same problem.
我问了完全相同的问题。
How can I make a XStreamMarshaller skip unknown binding?
如何使XStreamMarshaller跳过未知绑定?
And I got a comment linking this post.
我收到了一个链接这篇文章的评论。
I solved the my problem by extending the XStreamMarshaller
.
我通过扩展XStreamMarshaller解决了这个问题。
public class ExtendedXStreamMarshaller extends XStreamMarshaller {
@Override
protected void configureXStream(final XStream xstream) {
super.configureXStream(xstream);
xstream.ignoreUnknownElements(); // will it blend?
}
}
#1
16
As it took me more than 15 minutes to find this answer, I thought I would post it.
我花了15分钟才找到这个答案,我想我应该把它贴出来。
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.
这似乎跳过了对象中没有的xml项。
#2
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.
XStream 1.4.5支持处理未实现的标记。使用ignoreUnknownElements对那些尚未实现或已被删除且正在处理旧xml的标记。您还可以指定要忽略的特定标记。
#3
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:
首先,谢谢你分享这个答案。这是非常有用的。但是,上面提到的代码有问题。它没有@Override注释,这是使用这段代码所必需的。以下是更新后的代码:
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);
}
};
}
};
#4
0
From the x-stream FAQ:
从x-stream常见问题解答:
How does XStream deal with newer versions of classes?
XStream如何处理新版本的类?
- 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.
- 如果从类中删除字段,反序列化包含该字段的旧版本将导致异常。将字段保留在适当的位置,但将其声明为瞬态将避免异常,但是XStream不会尝试反序列化它。
- ...
- …
- implement a custom mapper to ignore unknown fields automatically (see acceptance test CustomMapperTest.testCanBeUsedToOmitUnexpectedElements())
- 实现一个自定义映射器来自动忽略未知字段(参见验收测试custommappertest.testcanbeusedtoomitunexpected tedelements ()))
#5
0
Since XStream 1.4.5 durring marshaller declaration it's enough to use ignoreEnknownElements() method:
由于XStream 1.4.5 durring marshaller声明时使用了ignoreEnknownElements()方法就足够了:
XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().ignoreUnknownElements();
...
to ignore unnecessary elements.
忽略不必要的元素。
#6
0
I asked for exactly the same problem.
我问了完全相同的问题。
How can I make a XStreamMarshaller skip unknown binding?
如何使XStreamMarshaller跳过未知绑定?
And I got a comment linking this post.
我收到了一个链接这篇文章的评论。
I solved the my problem by extending the XStreamMarshaller
.
我通过扩展XStreamMarshaller解决了这个问题。
public class ExtendedXStreamMarshaller extends XStreamMarshaller {
@Override
protected void configureXStream(final XStream xstream) {
super.configureXStream(xstream);
xstream.ignoreUnknownElements(); // will it blend?
}
}