Goal
I'm trying to generate a XML that validates against a given XSD by marshalling an object created with classes generated with said XSD.
Note that I cannot modify the XSD and I don't want to edit the generated classes.
Example
foo.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema>
<xs:element name="Foo" type="Foo"/>
<xs:complexType name="Foo">
<xs:sequence>
<xs:element name="Bar" type="Bar"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Bar">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="Baz" type="Max6Text"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="Max6Text">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="6"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Foo.java generated using jaxb2-maven-plugin
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Foo", propOrder = {
"bar"
})
public class Foo {
@XmlElement(name = "Bar", required = true)
protected Bar bar;
public Bar getBar() {
return bar;
}
public void setBar(Bar value) {
this.bar = value;
}
}
Bar.java generated using jaxb2-maven-plugin
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Bar", propOrder = {
"baz"
})
public class Bar {
@XmlElement(name = "Baz")
protected String baz;
public String getBaz() {
return baz;
}
public void setBaz(String value) {
this.baz = value;
}
}
Foo object instance creation
Foo foo = new ObjectFactory().createFoo();
Marshalling
File xmlResult = File.createTempFile("foo", ".xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(new ObjectFactory().createFoo(foo), new FileOutputStream(xmlResult));
String result = Files.readString(Path.of(xmlResult.getPath()));
System.out.println(result);
Problem
Expected output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo>
<Bar/>
</Foo>
Actual output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo/>
The problem is due to the fact that the bar
field of the Foo
object is null
, altough it's required = true
.
So, here is my question: How can I generate the Java classes with constructors that respect the required
field? I would have expected this as a generated Foo.java:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Foo", propOrder = {
"bar"
})
public class Foo {
@XmlElement(name = "Bar", required = true)
protected Bar bar;
public Foo() {
this.bar = new ObjectFactory().createBar();
}
public Bar getBar() {
return bar;
}
public void setBar(Bar value) {
this.bar = value;
}
}
I haven't found any configuration to change this in jaxb2-maven-plugin
.