java JAXB 忽略响应 XML 中的“额外”元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25621655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:20:47  来源:igfitidea点击:

JAXB Ignore 'extra' elements from Response XML

javaxmljaxbannotations

提问by Arpit Aggarwal

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 响应,并且它一直在非常频繁地变化(节点不断增加或减少)。在响应 xml 中的每次更新后,我的代码都会中断,因为我映射的 Java 类没有所有文件。

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

如果响应 XML 中发生任何更改,有什么方法可以避免我的代码中断。

Any help will be appreciated.

任何帮助将不胜感激。

Thanks.

谢谢。

采纳答案by Ian Roberts

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

应对未知字段,可以添加List<Object>属性注解@XmlAnyElement(lax=true)

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

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 JAXBContextyou'll get the unmarshalled form (the @XmlRootElementannotated class or a JAXBElement<Foo>), if the element is not known to the context you'll get an org.w3c.dom.Element.

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

Full details in Blaise's blog.

Blaise 的博客中的完整详细信息

For nodes that get removedyou should be fine as long as you use types that can be null (Integerrather than int, Booleanrather than boolean, etc).

对于被删除的节点,只要您使用可以为 null 的类型(Integer而不是intBoolean而不是boolean等),您应该没问题。

回答by icza

Use JAXB.unmarshal()to simply create Java objects from XML.

用于JAXB.unmarshal()从 XML 简单地创建 Java 对象。

By default it is very liberal.

默认情况下,它是非常自由的。

Quoting from the javadoc:

引用javadoc

In addition, the unmarshal methods have the following characteristic:

  1. Schema validation is not performed on the input XML. The processing will try to continue even if there are errors in the XML, as much as possible. Only as the last resort, this method fails with DataBindingException.

此外,解组方法具有以下特点:

  1. 不对输入 XML 执行架构验证。即使 XML 中有错误,处理也会尽可能地继续。仅作为最后的手段,此方法会因 DataBindingException 而失败。

So what JAXB.unmarshal()does is it tries to "transfer" as much data from XML to Java as possible, and it doesn't care if there is no Java field for an XML element or attribute, and it also doesn't care if there is a Java field for which there is no XML element or attribute.

那么JAXB.unmarshal()它的作用是尝试将尽可能多的数据从 XML“传输”到 Java,并且它不关心是否存在用于 XML 元素或属性的 Java 字段,它也不关心是否存在没有 XML 元素或属性的 Java 字段。

Example

例子

Let's try to unmarshal the following XML to an instance of java.awt.Point:

让我们尝试将以下 XML 解组为 的实例java.awt.Point

<p hi="Yo">
    <y>123</y>
    <peach>weor</peach>
</p>

The Java code:

Java代码:

String s = "<p hi=\"Yo\"><y>123</y><peach>weor</peach></p>";
Point p = JAXB.unmarshal(new StringReader(s), Point.class);
System.out.println(p); // Prints "java.awt.Point[x=0,y=123]"

We told JAXB.unmarshal()to parse a java.awt.Pointinstance. The input XML contains an element <y>which can be matched with Point.yso an intwas parsed and set to Point.y. No XML data was found for Point.xso it was not touched. There were no match for the attribute hiand the XML element <peach>, so they were simply not used for anything.

我们告诉JAXB.unmarshal()解析一个java.awt.Point实例。输入 XML 包含一个<y>可以与之匹配的元素,Point.y因此int被解析并设置为Point.y。没有找到 XML 数据,Point.x所以没有触及它。属性hi和 XML 元素不匹配<peach>,因此它们根本不用于任何事情。

We got absolutely no Exceptionhere, and the most that was possible was parsed and transferred from XML to Java.

我们在Exception这里完全没有,并且尽可能多地解析并从 XML 传输到 Java。