0
votes

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.

2

2 Answers

0
votes

It's a bit messy, but I was able to accomplish this with a small bash script:

xjc -p com.company.ustruc_to_xml OwnerDog.xsd
cd ./com/company/ustruc_to_xml
for f in *.java; do sed -i 's/\(^public class .*$\)/\1\n\n    private BookKeeper bk;/' $f; done
cp *.java /Users/car/Documents/workspace/proj_ustruc_to_xml/src/com/ibm/ustruc_to_xml
0
votes

I don't know a way to create the custom fields using JAXB.

I'd like to recommend you try to change the project's structure. I'd create a type which holds the JAXB data variable (ToyType or OwnerDog) and holds the BookKeeper as well. It may be a derived type.

Change the generated JAXB code manually can make the things harder if your system or your development team gets bigger.