2

I'm starting learning JAXB, so this question can be very silly. Now I have two classes, a base "base.java" and a derived class "child.java". These classes were generated using a ".xsd" file. I have another class "secondBase.java", this class was not generated by my ".xsd". My question is: Is it possible to use "secondBase.java" as a base for "child.java" instead of the base created by my Jaxb ?

Here's my .xsd file :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.1">

    <xs:complexType name="base">
        <xs:sequence>
            <xs:element name="id" type="xs:string" minOccurs="1"
                maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>
    <xs:element name="chlid" />
    <xs:complexType name="chlid">
        <xs:complexContent>
            <xs:extension base="base">
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

My secondBase.java :

public class secondBase {
    public secondBase(){
    id="0";
    }
    protected String id;
    public String getId() {
        return id;
    }
    public void setId(String value) {
        this.id = value;
    }
}

My base.java (generated using .xsd file)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "base", propOrder = {
    "id"
})
@XmlSeeAlso({
    Chlid.class
})
public class Base {

    @XmlElement(required = true)
    protected String id;
    public String getId() {
        return id;
    }
    public void setId(String value) {
        this.id = value;
    }

}

My child.java (generated using .xsd file)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "chlid")
public class Chlid
    extends Base
{


}

Thank's in advance :)

user6217189
  • 55
  • 1
  • 1
  • 8

1 Answers1

0

Here's the solution, it might be helpfull for someone .. I added this to my .xsd file :

  <xs:complexType name="secondBase">
    <xs:annotation>
      <xs:appinfo>
        <jaxb:class name="secondBase" implClass="com.myjaxb.classes.secondBase"/>
      </xs:appinfo>
    </xs:annotation>
  </xs:complexType>

And i Used this "complextype as an extention to my element :) PS : this answer is inspired from this topic : how to force schema compiled classes to extend specific class outside schema

Community
  • 1
  • 1
user6217189
  • 55
  • 1
  • 1
  • 8