1

我的 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>

生成的类符合预期(我删除了注释):

public abstract class Animal {
    protected String name;
    public String getName() {return name;}
    public void setName(String value) {this.name = value;}
}

public class CatType extends Animal {
    protected BigInteger nbOfLegs;
    public BigInteger getNbOfLegs() {
        if (nbOfLegs == null) {
            return new BigInteger("4");
        } else {
            return nbOfLegs;
        }
    }
    public void setNbOfLegs(BigInteger value) {this.nbOfLegs = value;}
}

这样,如果用户在 xml 中设置猫的腿数,它只能是 4,如果他不这样做,在代码中我无论如何都会收到 4。与袋鼠类似,我总是收到两条腿。
但是这种方法的问题是我不能像这样使用多态:

for(Animal animal : animals) {
    System.out.println(animal.getNbOfLegs());
}


所以我尝试了一种不同的方法:

<?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>

Animal 类按预期生成:

public abstract class Animal {  
    protected String name;
    protected BigInteger nbOfLegs;
    public String getName() {return name;}
    public void setName(String value) {this.name = value;}
    public BigInteger getNbOfLegs() {return nbOfLegs;}
    public void setNbOfLegs(BigInteger value) {this.nbOfLegs = value;}
}

但是生成的 CatType是空的。
我希望它是这样的:

public class CatType extends Animal {
    @Override
    public BigInteger getNbOfLegs() {
        if (nbOfLegs == null) {
            return new BigInteger("4");
        } else {
            return nbOfLegs;
        }
    }
}

是否可以自定义绑定文件以实现所需的生成 CatType 类?
谢谢你。

4

0 回答 0