JAXB:在不同的类

时间:2015-12-14 15:06:00

标签: java xsd jaxb

我有如下的XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.0">
    <xs:element name="Test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="command">
                    <xs:complexType>
                        <xs:choice minOccurs="0" maxOccurs="1">
                            <xs:sequence>
                                <xs:element name="abc" type="xs:integer"/>
                                <xs:element name="aaa" type="xs:string"/>
                                <xs:element name="foo" type="xs:string"/>
                                <xs:element name="bar" type="xs:string"/>
                            </xs:sequence>
                            <xs:sequence>
                                <xs:element name="foo" type="xs:string"/>
                                <xs:element name="bar" type="xs:string"/>
                                <xs:element name="bbb" type="xs:string"/>
                                <xs:element name="abc" type="xs:integer"/>
                                <xs:element name="ccc" type="xs:string" minOccurs="0"/>
                                <xs:element name="ddd" type="xs:string" minOccurs="0"/>
                            </xs:sequence>
                        </xs:choice>
                        <xs:attribute name="name" type="xs:string" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

JAXB生成一个通用的内容字段,这不是很方便:

    @XmlElementRefs({
        @XmlElementRef(name = "ddd", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "abc", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "bbb", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "aaa", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "bar", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "foo", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "ccc", type = JAXBElement.class, required = false)
    })
    protected List<JAXBElement<? extends Serializable>> content;

我希望2个序列有2个单独的类,包含相应的属性。

请注意,我无法更改XML结构。我更喜欢自定义绑定解决方案(使用xjb文件),但我也可以更改XSD,只要它不会改变XML结构。

EG。我试过了:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.0">
    <xs:element name="Test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="command">
                    <xs:complexType>
                        <xs:choice minOccurs="0" maxOccurs="1">
                            <xs:group ref="AGroup"/>
                            <xs:group ref="BGroup"/>
                        </xs:choice>
                        <xs:attribute name="name" type="xs:string" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:group name="AGroup">
        <xs:sequence>
            <xs:element name="abc" type="xs:integer"/>
            <xs:element name="aaa" type="xs:string"/>
            <xs:element name="foo" type="xs:string"/>
            <xs:element name="bar" type="xs:string"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="BGroup">
        <xs:sequence>
            <xs:element name="foo" type="xs:string"/>
            <xs:element name="bar" type="xs:string"/>
            <xs:element name="bbb" type="xs:string"/>
            <xs:element name="abc" type="xs:integer"/>
            <xs:element name="ccc" type="xs:string" minOccurs="0"/>
            <xs:element name="ddd" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:group>
</xs:schema>

但JAXB生成相同的代码。

0 个答案:

没有答案