Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating a JAXB class that implements an interface
    text
    copied!<p>I'm currently using JAXB to generate java classes in order to unmarshall XML. Now I would like to create a new schema very similar to the first and have the classes that are generated implement the same interface.</p> <p>Say for example, I have two schema files which define XML with similar tags:</p> <p>adult.xsd</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:element name="Person"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="Name" type="xs:string" /&gt; &lt;xs:element name="Age" type="xs:integer" /&gt; &lt;xs:element name="Job" type="xs:string" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; </code></pre> <p>kid.xsd</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:element name="Person"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element name="Name" type="xs:string" /&gt; &lt;xs:element name="Age" type="xs:integer" /&gt; &lt;xs:element name="School" type="xs:string" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; </code></pre> <p>Using JAXB and XJC I'd like to generate two class files:</p> <pre><code>public class Adult implements Person { ... public String getName() { ... } public int getAge() { ... } public String getJob { ... } } public class Kid implements Person { ... public String getName() { ... } public int getAge() { ... } public String getSchool { ... } } </code></pre> <p>where the Person interface defines the <code>getName()</code> and <code>getAge()</code> methods.</p> <p>I've looked at some of the <a href="https://jaxb.dev.java.net/guide/Mapping_interfaces.html" rel="noreferrer">documentation</a> for mapping interfaces but this appears to only be for the situation when you already have java classes that you want to map to a DOM.</p> <p>Also, I've tried to use this <a href="http://xml.w-wins.com/xjc-plugins/interfaces/" rel="noreferrer">external plugin</a> but it doesn't appear to work. Here is my xjb binding file:</p> <pre class="lang-xml prettyprint-override"><code>&lt;jxb:bindings version="1.0" xmlns:jxb="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:ext="http://xml.w-wins.com/xjc-plugins/interfaces" jxb:extensionBindingPrefixes="xjc"&gt; &lt;jxb:bindings schemaLocation="xsd/adult.xsd" node="xs:schema/xs:complexType[@name='Person']"&gt; &lt;ext:interface&gt;mypackage.Hello&lt;/ext:interface&gt; &lt;/jxb:bindings&gt; &lt;/jxb:bindings&gt; </code></pre> <p>but this gives the following error:</p> <pre class="lang-none prettyprint-override"><code>$ java -cp "lib/activation.jar;lib/InterfacesXJCPlugin.jar;lib/jaxb1-impl.jar;lib/jaxb-api.jar;lib/jaxb-xjc.jar;lib/jsr173_1.0_api.jar" com.sun.tools.xjc.XJCFacade -p mypackage.myxml -extension -Xinterfaces xsd/adult.xsd -b binding.xjb parsing a schema... [ERROR] XPath evaluation of "xs:schema/xs:complexType[@name='Person']" results in empty target node line 8 of file:/C:/dev/jaxb/jaxb-ri/binding.xjb Failed to parse a schema. </code></pre> <p>Is it possible to generate a class with JAXB that implements an interface?</p> <p><strong>Update</strong></p> <p>I've tried using the <a href="https://jaxb2-commons.dev.java.net/interface-insertion/" rel="noreferrer">Interface Insertion</a> plugin but for some reason can't get it to work. This is how I'm calling xjc yet it is as if the plugin jar is not getting picked up from the classpath:</p> <pre class="lang-none prettyprint-override"><code>$ java -cp "lib/xjc-if-ins.jar;lib/jaxb-xjc.jar" com.sun.tools.xjc.XJCFacade -p mypackage -extension -Xifins myschema.xsd -b binding.xjb </code></pre> <p>I get the error:</p> <pre class="lang-none prettyprint-override"><code>unrecognized parameter -Xifins </code></pre> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload