转换nil =" true"在unmarshal操作期间为null [英] Convert nil="true" to null during unmarshal operation

查看:114
本文介绍了转换nil =" true"在unmarshal操作期间为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个服务器接收XML,该服务器的模式几乎将每个元素指定为:

I am receiving XML from a server whose schema specifies nearly every element as:

<xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="myIntElementName" type="xs:int" nillable="true" minOccurs="0"/>

我正试图找到一种干净的方式来转换我收到的标记为<的每个元素code> xsi:nil =true在将其解组到JAXB对象中时为null。这样的事情:

I'm trying to find a clean way to convert every element that I receive that is marked as xsi:nil="true" to a null when it is unmarshalled into a JAXB object. So something like this:

<myIntElementName xsi:nil="true" />

应该导致我的JAXB对象具有 myIntElementName 属性值为null,而不是 JAXBElement 对象(或沿着这些行的任何内容)。我对发送给我使用 nillable 属性的XML的系统没有任何控制权,所以当我收到它时,我需要在我的最后转换它。

Should result in my JAXB object having a myIntElementName property with a value of null, rather than a JAXBElement object with a nil property set to true (or anything along those lines). I don't have any control over the system that is sending me the XML that uses the nillable attribute, so I need to convert this on my end when I receive it.

推荐答案

@XmlElement(nillable = true)

您只需在字段/属性上指定 @XmlElement(nillable = true)即可获得此行为:

You just need to specify @XmlElement(nillable=true) on your field/property to get this behaviour:

@XmlElement(nillable=true)
private String foo;






从XML架构生成

下面我将演示如何从XML模式开始生成此映射。

Below I'll demonstrate how to generate this mapping if you are staring from an XML schema.

XML架构(schema.xsd)

XML Schema (schema.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="foo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="myStringElementName" type="xs:string"
                    nillable="true" minOccurs="0" />
                <xs:element name="myIntElementName" type="xs:int"
                    nillable="true" minOccurs="0" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

为什么要获得类型为的属性JAXBElement

生成类型 JAXBElement 的属性你的模型,因为你有一个可以为nillable的元素,这是 minOccurs =0。使用 JAXBElement 允许模型区分缺少的元素(属性为null)和元素的存在 nil =true(设置了nil标志的JAXBElement)。

A property of type JAXBElement is generated in your model because you have a nillable element this is minOccurs="0". The use of JAXBElement allows the model to differentiate between an missing element (property is null) and the presence of the element with nil="true" (JAXBElement with nil flag set).

<xs:element name="myStringElementName" type="xs:string"
                        nillable="true" minOccurs="0" />

外部绑定文件(binding.xml)

External Binding File (binding.xml)

可以指定外部绑定文件来告诉JAXB实现不生成 JAXBElement 类型的任何属性。请注意,这将使JAXB无法将所有XML文档往返。

An external binding file can be specified to tell the JAXB implementation not to generate any properties of type JAXBElement. Note this will make it impossible for JAXB to round trip all XML documents.

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.0"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false"/>
    </jaxb:bindings>
</jaxb:bindings>

XJC致电

XJC Call

以下是如何利用XJC Call中的外部绑定文件的示例

Below is an example of how to leverage an external binding file from the XJC Call

xjc -b binding.xml schema.xsd

生成模型(Foo)

Generated Model (Foo)

生成的模型如下所示:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "myStringElementName",
    "myIntElementName"
})
@XmlRootElement(name = "foo")
public class Foo {

    @XmlElement(nillable = true)
    protected String myStringElementName;
    @XmlElement(nillable = true)
    protected Integer myIntElementName;

    public String getMyStringElementName() {
        return myStringElementName;
    }

    public void setMyStringElementName(String value) {
        this.myStringElementName = value;
    }

    public Integer getMyIntElementName() {
        return myIntElementName;
    }

    public void setMyIntElementName(Integer value) {
        this.myIntElementName = value;
    }

}






了解更多信息

  • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html

这篇关于转换nil =&quot; true&quot;在unmarshal操作期间为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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