JAXB XJC代码生成元素初始化器及其声明 [英] JAXB XJC code generation of element initializers with their declaration

查看:77
本文介绍了JAXB XJC代码生成元素初始化器及其声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下架构:

<xs:element name="Book">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Chapter" />
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="Chapter">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" name="Word" />
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="Word">
</xs:element>

它会生成如下内容:

@XmlRootElement(name = "Book")
public class Book {

    @XmlElement(name = "Chapter", required = true)
    protected Chapter chapter;

是否可以生成以下内容?

Is it possible to generate the following instead?

@XmlElement(name = "Chapter", required = true)
protected Chapter chapter = new Chapter();

即使XML文件缺少Book中的Chapter元素,它也是如此unmarshalled仍然会创建一个Book对象,因此可以执行
book.getChapter()。getWord()并检索一个空列表,而不是检查null。

This is so that even if an XML file is missing a Chapter element within a Book, when it is unmarshalled there will still be a Book object created so it is possible to do book.getChapter().getWord() and retrieve an empty list, instead of checking for null.

推荐答案

您可以创建一个插件。我写过简短教程你这样做。希望你觉得它有用。

You can create a plugin. I've written a short tutorial that helps you do exactly that. Hope you find it helpful.

这篇关于JAXB XJC代码生成元素初始化器及其声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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