Java JAXB为子类型中的固定属性生成了类

Java JAXB为子类型中的固定属性生成了类,java,binding,jaxb,xsd,generated-code,Java,Binding,Jaxb,Xsd,Generated Code,我的java类是从xsd文件生成的。 我想要实现的目标是基于元素的类型拥有一些已知的属性。例如,我有一份动物名单。解析xml后,我想知道代码中有多少条腿是我的动物。但是腿的数量是动物类型的一个特征,因此,如果我有一只猫,它将有4条腿,袋鼠将有2条腿。 如果我这样定义xsd: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFo

我的java类是从xsd文件生成的。 我想要实现的目标是基于元素的类型拥有一些已知的属性。例如,我有一份动物名单。解析xml后,我想知道代码中有多少条腿是我的动物。但是腿的数量是动物类型的一个特征,因此,如果我有一只猫,它将有4条腿,袋鼠将有2条腿。 如果我这样定义xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="Animals" type="animals" />

    <xs:complexType name="animals">
        <xs:sequence>
            <xs:element name="Animal" maxOccurs="unbounded"  type="animal"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="animal" abstract="true">
        <xs:simpleContent>
            <xs:extension base="xs:string">
             <xs:attribute name="name" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="catType">
        <xs:simpleContent>
            <xs:extension base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="4" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="kangarooType">
        <xs:simpleContent>
            <xs:extension base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="2" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>
for(Animal animal : animals) {
    System.out.println(animal.getNbOfLegs());
}
public class CatType extends Animal {
    @Override
    public BigInteger getNbOfLegs() {
        if (nbOfLegs == null) {
            return new BigInteger("4");
        } else {
            return nbOfLegs;
        }
    }
}
这样,如果用户在xml中设置猫的腿数,那么它只能是4,如果他没有设置,那么在代码中我将收到4。袋鼠也一样,我总是有两条腿。 但这种方法的问题是,我不能像这样使用多元论:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="Animals" type="animals" />

    <xs:complexType name="animals">
        <xs:sequence>
            <xs:element name="Animal" maxOccurs="unbounded"  type="animal"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="animal" abstract="true">
        <xs:simpleContent>
            <xs:extension base="xs:string">
             <xs:attribute name="name" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="catType">
        <xs:simpleContent>
            <xs:extension base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="4" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="kangarooType">
        <xs:simpleContent>
            <xs:extension base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="2" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>
for(Animal animal : animals) {
    System.out.println(animal.getNbOfLegs());
}
public class CatType extends Animal {
    @Override
    public BigInteger getNbOfLegs() {
        if (nbOfLegs == null) {
            return new BigInteger("4");
        } else {
            return nbOfLegs;
        }
    }
}
所以我尝试了一种不同的方法:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="Animals" type="animals" />

    <xs:complexType name="animals">
        <xs:sequence>
            <xs:element name="Animal" maxOccurs="unbounded"  type="animal"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="animal" abstract="true">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="name" />
                <xs:attribute name="nbOfLegs" type="xs:integer"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="catType">
        <xs:simpleContent>
            <xs:restriction base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="4" />
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="kangarooType">
        <xs:simpleContent>
            <xs:restriction base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="2" />
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>
但是生成的CatType是空的。 我以为会是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="Animals" type="animals" />

    <xs:complexType name="animals">
        <xs:sequence>
            <xs:element name="Animal" maxOccurs="unbounded"  type="animal"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="animal" abstract="true">
        <xs:simpleContent>
            <xs:extension base="xs:string">
             <xs:attribute name="name" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="catType">
        <xs:simpleContent>
            <xs:extension base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="4" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="kangarooType">
        <xs:simpleContent>
            <xs:extension base="animal">
                <xs:attribute name="nbOfLegs" type="xs:integer" fixed="2" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>
for(Animal animal : animals) {
    System.out.println(animal.getNbOfLegs());
}
public class CatType extends Animal {
    @Override
    public BigInteger getNbOfLegs() {
        if (nbOfLegs == null) {
            return new BigInteger("4");
        } else {
            return nbOfLegs;
        }
    }
}
是否可以自定义绑定文件以获得所需的生成CatType类?
谢谢。

我也有同样的问题,你是如何解决的?我的问题还在于,我不能修改模式,但只能修改绑定文件。我无法使其工作,因此我决定手动修改生成的文件并对其进行版本设置。这样我就可以为它添加更多的功能。当然,最大的缺点是,当修改模式时,必须手动将版本化文件与新生成的文件进行比较,并进行相应的修改。感谢您的回答,据我在一些网站上看到的,这个问题现在没有解决方案,因为在规范中有任何解决方案:/