Aravind Datta
Aravind Datta

Reputation: 327

how to customize and override jaxb bindings

I am working with a set of DTDs from a third party system. Our goal is to map the XML request (which conforms to those DTDs into java and then, send an XML response back to the system).

DTDs are written in stone (I don't have any control in changing those).

So, in order to map, I converted DTDs to XML Schemas (xsd) using XMLSpy and then, created Jaxb binding classes using XJC compiler. I am using Java 7.

The problem is, the DTDs don't really have a namespace.. and I have 20 different DTDs.. 10 for request and 10 for response. When I generated the schemas, I had to do one-one mapping.. and created the same 10 request XSDs and 10 response XSDs.

Now, the jaxb xjc compiler generated binding classes.. but they are far from practical use. There is no inheritance 'cus these schemas are not related to each other (although they seem to have similar content - request types and response types).

Can someone please help me if there is a way to customize jaxb bindings to override the default bindings and create more reasonable bindings?

For example consider this simple case:

DTD:

<!ELEMENT FromDate (#PCDATA)>
<!ATTLIST FromDate
    year CDATA #REQUIRED
    month CDATA #REQUIRED
    day CDATA #REQUIRED
>

Schema that I generated using XMLSpy:

<xs:element name="FromDate">
    <xs:complexType mixed="true">
        <xs:attribute name="year" use="required"/>
        <xs:attribute name="month" use="required"/>
        <xs:attribute name="day" use="required"/>
    </xs:complexType>
</xs:element>

The binding classes that generated out of XJC compiler (java 1.7):

public class FromDate {

    @XmlValue
    protected String content;
    @XmlAttribute(name = "year", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String year;
    @XmlAttribute(name = "month", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String month;
    @XmlAttribute(name = "day", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String day;
    ...
    ...

If you look at how fromDate finally evolved, it doesn't make any sense 'cus just to get the date from this request, I need to do

setMyDate(request.getFromDate().getMonth() + request.getFromDate().getDay() + request.getFromDate().getYear());

which obviously doesn't make sense. Plus, the types are way off.

How can I customize/override jaxb bindings to achieve these two things: 1. inheritance (some kind of abstraction to reduce redundancy) 2. appropriate types

Please help.

Upvotes: 2

Views: 1534

Answers (1)

lexicore
lexicore

Reputation: 43709

OMG someone tries to compile DTDs in 2014. :)

Few links for you:

As another approach I'd suggest converting DTDs to schemas an processing schemas. Will be better long-term. DTD support is quite limited

Upvotes: 1

Related Questions