简体   繁体   English

是否可以将现有类作为.xsd文件jaxb中的基础引用

[英]Is it possible to refer to an existing class as a base in .xsd file jaxb

I'm starting learning JAXB, so this question can be very silly. 我开始学习JAXB,所以这个问题可能很愚蠢。 Now I have two classes, a base "base.java" and a derived class "child.java". 现在,我有两个类,基本类“ base.java”和派生类“ child.java”。 These classes were generated using a ".xsd" file. 这些类是使用“ .xsd”文件生成的。 I have another class "secondBase.java", this class was not generated by my ".xsd". 我有另一个类“ secondBase.java”,该类不是由我的“ .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 ? 我的问题是:是否可以将“ secondBase.java”用作“ child.java”的基础,而不是我的Jaxb创建的基础?

Here's my .xsd file : 这是我的.xsd文件:

<?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 : 我的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) 我的base.java(使用.xsd文件生成)

@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) 我的child.java(使用.xsd文件生成)

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


}

Thank's in advance :) 提前致谢 :)

Here's the solution, it might be helpfull for someone .. I added this to my .xsd file : 这是解决方案,可能对某人有所帮助..我将其添加到我的.xsd文件中:

  <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 我使用这个“复杂类型”作为对元素的扩展:) PS:这个答案的灵感来自于这个主题: 如何强制模式编译类将特定类扩展到模式之外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM