Java XStream - 忽略XML中不存在的标记 [英] Java XStream - Ignore tag that doesn't exist in XML

查看:823
本文介绍了Java XStream - 忽略XML中不存在的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是一段XML,如下所示

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>

我坚持使用的是,当使用XStream时,我需要能够忽略出现的某些标签(在'FavOS'上面的情况下)
这些标签可能未知或将来发生变化。有没有办法忽略所有与当前实现的不匹配的标签?

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 1.3.1)

(Using XStream 1.3.1)

推荐答案

我花了超过15分钟才找到这个答案,我以为我会发布它。

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

这似乎跳过了你对象中没有的xml项目。

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

这篇关于Java XStream - 忽略XML中不存在的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆