JAXB XJC code generation of element initializers with their declaration

If I have a scheme like:

<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 create the following instead:

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

This is so that even if there is no Chapter element inside the Book in the XML file when it is not reinforced, the Book object will still be created, so you can make book.getChapter (). getWord () and get an empty list instead of checking for zero.

+7
source share
1 answer

You can create a plugin. I wrote a short tutorial to help you do just that. I hope you will like it.

0
source

All Articles