JAXB忽略Response XML中的'额外'元素 [英] JAXB Ignore 'extra' elements from Response XML

查看:519
本文介绍了JAXB忽略Response XML中的'额外'元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个XML响应,它不断变化(节点不断增加或减少)。在响应xml的每次更新之后,我的代码断开,因为我的映射Java类没有所有文件。

I am getting a XML response and it keeps on changing very frequently (nodes keep on increasing or reducing). After each updation in response xml my code breaks as my mapped Java class does not have all fileds.

如果响应中发生任何更改,有没有办法避免我的代码中断XML。

Is there any way to avoid my code breaking if any changes occurs in response XML.

任何帮助都将不胜感激。

Any help will be appreciated.

谢谢。

推荐答案

要处理未知字段,您可以添加列表< Object> 属性注释 @XmlAnyElement(lax = true)

To cope with unknown fields, you can add a List<Object> property annotated @XmlAnyElement(lax=true)

@XmlAnyElement(lax = true)
private List<Object> anything;

输入中与该类的显式属性不对应的任何元素都会被扫描到此名单。如果 JAXBContext 知道该元素,您将获得未编组的表单( @XmlRootElement 带注释的类或 JAXBElement< Foo> ),如果上下文不知道该元素,您将获得 org.w3c.dom.Element

Any elements in the input that do not correspond to explicit properties of the class will be swept up into this list. If the element is known to the JAXBContext you'll get the unmarshalled form (the @XmlRootElement annotated class or a JAXBElement<Foo>), if the element is not known to the context you'll get an org.w3c.dom.Element.

Blaise的全部细节博客

对于删除的节点,只要使用可以为null的类型(<$),就应该没问题c $ c>整数而不是 int 布尔而不是 boolean 等。)

For nodes that get removed you should be fine as long as you use types that can be null (Integer rather than int, Boolean rather than boolean, etc).

这篇关于JAXB忽略Response XML中的'额外'元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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