Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add interface on autogenerated jaxb elements by xsdtojava?

Tags:

java

xml

jaxb

xjc

I want to add a super interface for a class autogenerated by jaxb and xsdtojava.

Problem: I can only add the interface on the root element (which I don't want, but just for testing purpose).

The element where I want to apply the inheritance is thelistelement.

The xsd I have not control of.

<xs:schema>
    <xs:element name="myRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="thelist">
                   <xs:complexType>
                      <xs:sequence>
                         <xs:element name="thelistelement" maxOccurs="unbounded">
                    ...

binding file:

<jaxb:bindings    
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jaxb:extensionBindingPrefixes="xjc inheritance"
    jaxb:version="2.1">

    <!-- this works -->
    <jaxb:bindings schemaLocation="xsd/my.xsd">
        <jaxb:bindings node="//xs:element[@name='myRequest']">
                    <inheritance:implements>MyInterface</inheritance:implements>
        </jaxb:bindings>
    </jaxb:bindings>

    <!-- this does NOT work -->
    <jaxb:bindings schemaLocation="xsd/my.xsd">
        <jaxb:bindings node="//xs:element[@name='thelistelement']">
                    <inheritance:implements>MyInterface</inheritance:implements>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

Running with <extensionArg>-Xinheritance</extensionArg>.

Output should be:

...
List<Thelistelement> thelist;

class Thelistelement implements MyInterface {

}

But the interface is missing on the list element class. Why then does it work on the root element myRequest?

like image 858
membersound Avatar asked Oct 18 '22 02:10

membersound


People also ask

How do you auto generate JAXB classes?

To use the JAXB class generator to generate Java classes you must provide it with an XML schema. DTDs are not supported by JAXB. As explained in "Converting DTDs to XML Schemas", however, you can use the DTD2Schema program to convert a DTD to an XML schema.

What is XJB file in JAXB?

Generally we create a bindings file with . xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

What is use of XSD file in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.


1 Answers

I had to add an /xs:complexType to the node. Don't know why that worked though.

Important to note the single slash before the complexType!

<jaxb:bindings schemaLocation="xsd/my.xsd">
    <jaxb:bindings node="//xs:element[@name='thelistelement']/xs:complexType">
                <inheritance:implements>MyInterface</inheritance:implements>
    </jaxb:bindings>
</jaxb:bindings>
like image 152
membersound Avatar answered Oct 21 '22 02:10

membersound