如何在反序列化文档时忽略未使用的XML元素? [英] How to ignore unused XML elements while deserializing a document?

查看:140
本文介绍了如何在反序列化文档时忽略未使用的XML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimpleXml来(de)序列化POJO。现在,我有一个大的XML,它有一些不需要的元素。例如,使用此XML:

I'm using SimpleXml to (de)serialize POJOs. Now, I have a big XML which has some elements which are not needed. For instance, with this XML:

<Root>
   <Element>Used</Element>
   <Another>Not used</Another>
<Root> 

我想创建一个POJO,如下所示:

I want to create a POJO which looks like:

@Root
class Root{
    @Element
    private String element;
}

问题是我得到了这个例外:

Problem is that I'm getting this Exception:

simpleframework.xml.core.ElementException: Element 'Another' does not have a
match in class blah.blah.Blah at line 1

那么......我应该如何配置POJO以便我能正确解析XML?

So... how should I configure the POJO so that I can parse the XML correctly?

推荐答案

在Root注释中将 strict 设置为false以忽略任何XML元素或属性没有出现在课堂上。

Set strict to false within the Root annotation to ignore any XML elements or attributes that do not appear in the class.

@Root(strict=false)

或者,在读取xml时将 strict 设置为false 序列化程序

Alternatively, set strict to false when you read the xml in the serialiser:

Root root = serializer.read(Root.class, source, false);

这篇关于如何在反序列化文档时忽略未使用的XML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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