JAXB XJC code generation of element initializers with their declaration

泄露秘密 提交于 2019-12-22 05:06:57

问题


If I have a schema such as the following:

<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>

It will generate something like:

@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();

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.


回答1:


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



来源:https://stackoverflow.com/questions/13250913/jaxb-xjc-code-generation-of-element-initializers-with-their-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!