JAXB xjc:如何为字符串生成代码(如果该值为空,则返回空)? [英] JAXB xjc: How to generate code for Strings that returns empty if the value is null?

查看:51
本文介绍了JAXB xjc:如何为字符串生成代码(如果该值为空,则返回空)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下示例xsd片段:

Given the following example xsd snippet:

< xs:attribute name="SEGMENT"  default="" use="optional" type="xs:string"/ >

当xjc生成包含SEGMENT bean属性的类时,将自动生成以下getter:

when xjc generates the class containing the SEGMENT bean attribute, the following getter is auto-generated:

public String getSEGMENT() {
    if (segment == null) {
        return "";
    } else {
        return segment;
    }
}

我的问题是您如何对xs:element对象执行相同的操作?换句话说,给出以下xsd片段:

My question is how do you get it do the same for xs:element objects? In other words, given the following xsd snippet:

< xs:element name="NAME" default="" type="xs:string"/ >

我想知道是否可以让xjc生成以下内容:

I want to know if I can get xjc to generate the following:

public String getNAME() {
    if (name == null) {
        return "";
    } else {
        return name;
    }
}

这怎么办?

推荐答案

JAXB不会为具有默认值的元素生成与为具有默认值的属性生成相同的代码,因为

JAXB doesn't generate the same code for an element with default value as it does for an attribute with default value because the XML schema differentiates between element and attribute defaults:

使用默认属性声明属性和元素的默认值,尽管此属性在每种情况下的结果略有不同.当用默认值声明属性时,该属性的值就是在实例文档中作为该属性的值出现的任何值;如果该属性未出现在实例文档中,则模式处理器将为该属性提供一个值,该值等于默认属性的值.请注意,仅当属性本身是可选属性时,属性默认值才有意义,因此同时指定默认值和除可选值之外的任何其他内容以供使用是错误的.

Default values of both attributes and elements are declared using the default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute's value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of optional for use.

模式处理器对默认元素的处理略有不同.当使用默认值声明元素时,该元素的值就是在实例文档中作为元素内容出现的任何值;如果该元素显示为没有任何内容,则模式处理器将为该元素提供一个值,该值等于默认属性的值.但是,如果该元素没有出现在实例文档中,那么架构处理器将根本不提供该元素.总之,元素默认值和属性默认值之间的差异可以表示为:当缺少属性时,默认属性值适用;当元素为空时,默认元素值适用.

The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.

您总是可以依靠缺少属性的默认值(从这里获取特殊的吸气剂),但是有一个缺少元素值的陷阱.

You can always count on the default value for a missing attribute (from here the special getter) but there is a catch with a missing element value.

尽管如此,当您解组一个实例时,解组器知道如何处理默认值.有关详情,请参见此处:

Nonetheless, when you unmarshall an instance, the unmarshaller knows how to handle the default value. See here for details:

  • Element default values and marshalling
  • Element default values and unmarshalling

XJC不会添加吸气剂代码或使用默认值初始化字段,因此,如果需要"null safe check",您可以在XJC生成代码后自己手动添加它,也可以尝试使用某些插件自动执行该操作:

XJC won't add the getter code or initialize the fields with the default value, so if you need the "null safe check" you can either add it yourself manually after the code is generated by XJC or try to use some plugin to do it automatically:

  • JAXB 2 Default Value Plugin
  • CXF XJC Default Value Plugin

这篇关于JAXB xjc:如何为字符串生成代码(如果该值为空,则返回空)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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