Java XStream - 忽略 XML 中不存在的标签

发布于 2024-10-06 21:24:50 字数 457 浏览 9 评论 0原文

我目前使用如下的一段 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>

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

(使用XStream 1.3.1)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

画离情绘悲伤 2024-10-13 21:24:50

由于我花了超过 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;
                        }
                    }
                };
            }
        };

这似乎会跳过不在您的对象中的 xml 项目。

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.

深居我梦 2024-10-13 21:24:50

XStream 1.4.5 支持处理未实现的标签。使用 ignoreUnknownElements 来标记尚未实现或已被删除,并且您正在处理旧的 xml。您还可以指定要忽略的特定标签。

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.

马蹄踏│碎落叶 2024-10-13 21:24:50

首先,感谢您分享这个答案。这非常有用。但是,上面提到的代码有问题。它没有 @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);
              }
            };
          }
        };

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);
              }
            };
          }
        };
画骨成沙 2024-10-13 21:24:50

来自 x-stream 常见问题解答

XStream如何处理newer
类的版本?

  • 如果类中添加了新字段,则反序列化旧版本
    将使该字段处于未初始化状态。
  • 如果从类中删除某个字段,则反序列化包含该字段的旧版本将导致异常。保留该字段但将其声明为瞬态将避免异常,但 XStream 不会尝试反序列化它。
  • ...
  • 实现自定义映射器以自动忽略未知字段(请参阅验收测试 CustomMapperTest.testCanBeUsedToOmitUnexpectedElements())

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())
瞳孔里扚悲伤 2024-10-13 21:24:50

自 XStream 1.4.5 起,在编组器声明期间使用ignoreEnknownElements() 方法就足够了:

XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().ignoreUnknownElements();
...

忽略不必要的元素。

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.

宣告ˉ结束 2024-10-13 21:24:50

我问了完全相同的问题。

如何让 XStreamMarshaller 跳过未知绑定?

我收到了一条链接到这篇文章的评论。

我通过扩展 XStreamMarshaller 解决了我的问题。

public class ExtendedXStreamMarshaller extends XStreamMarshaller {

    @Override
    protected void configureXStream(final XStream xstream) {
        super.configureXStream(xstream);
        xstream.ignoreUnknownElements(); // will it blend?
    }
}

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?
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文