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

查看:39
本文介绍了JAXB 忽略响应 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.

Blaise 博客中的详细信息.

对于被删除的节点,只要您使用可以为空的类型(Integer 而不是 intBoolean 而不是 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屋!

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