I am using the JAXB xjc command line tool to convert an XML schema (.xsd file) into Java objects.
Here are the contents of my .xsd file:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ownerDog">
<xs:complexType>
<xs:sequence>
<xs:element name="owner" type="xs:string" />
<xs:element name="dog" type="xs:string"/>
<xs:element name="toy" type="toyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="toyType">
<xs:sequence>
<xs:element name="color" type="xs:string" />
<xs:element name="price" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:schema>
When I run .xsd, I get three classes: OwnerDog.java, ToyType.java, and ObjectFactory.java.
I would like to add a custom field to OwnerDog.java and ToyType.java. This field will be a reference to a bookkeeping class which I will use to track statistics for each node in my XML tree.
For example, this is the code I get for ToyType.java:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "toyType", propOrder = {
"color",
"price"
})
public class ToyType {
@XmlElement(required = true)
protected String color;
@XmlElement(required = true)
protected BigInteger price;
public String getColor() {
return color;
}
public void setColor(String value) {
this.color = value;
}
public BigInteger getPrice() {
return price;
}
public void setPrice(BigInteger value) {
this.price = value;
}
}
I would like it to look like this:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "toyType", propOrder = {
"color",
"price"
})
public class ToyType {
private BookKeeper bk;
@XmlElement(required = true)
protected String color;
@XmlElement(required = true)
protected BigInteger price;
public String getColor() {
return color;
}
public void setColor(String value) {
this.color = value;
}
public BigInteger getPrice() {
return price;
}
public void setPrice(BigInteger value) {
this.price = value;
}
}
Is there a way to add fields to JAXB-generated classes programmatically? Obviously, for my example it is easy enough to do it manually, but this is just a practice problem for a much larger production problem.
I am not able to modify the original .xsd file, and I will need this to work for multiple .xsd files which I have not seen yet.