JAXB 忽略响应 XML 中的“额外"元素 [英] JAXB Ignore 'extra' elements from 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.
任何帮助将不胜感激.
谢谢.
推荐答案
应对未知字段,可以添加List
属性注解@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
),如果上下文不知道该元素,您将获得一个 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
.
对于被删除的节点,只要您使用可以为空的类型(Integer
而不是 int
、Boolean
而不是 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 忽略响应 XML 中的“额外"元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!