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