Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

java - JAXB List Tag creating inner class

So we have an XSD type in the form:

<xs:complexType name="Foo">
    <xs:all>
        <xs:element name="Bars">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Bar" type="barType" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:all>
</xs:complexType>    

to represent XML:

<Foo>
    <!-- Elements snipped for brevity-->
    <Bars>
        <Bar>
            <!-- Bar Element -->
        </Bar>
    </Bars>
</Foo>

xjc produces almost correct results. The only annoying thing is that "Bars" is created as an inner class which stores a list of Bars. Is there anyway to have Bars be a List in Foo while still retaining the XML above?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you define Bars as a complex type, Bars will be generated as separated class. Like this I find schema also easier to read. Bars will not be List in Foo unless you change maxOccurs to a value higher than 1 - you cannot do this on xs:all but you can use xs:sequence.

...
    <xs:complexType name="Foo">
        <xs:all>
            <xs:element name="Bars" type="Bars" />
        </xs:all>
    </xs:complexType>

    <xs:complexType name="Bars">
        <xs:sequence>
            <xs:element name="Bar" type="barType" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
...

After running xjc: Foo.java:

    ...
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Foo", propOrder = {

    })
    public class Foo {

        @XmlElement(name = "Bars", required = true)
        protected Bars bars;

        public Bars getBars() {
            return bars;
        }

        public void setBars(Bars value) {
            this.bars = value;
        }
    }

Bars.java:

    ...
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Bars", propOrder = {
        "bar"
    })
    public class Bars {

        @XmlElement(name = "Bar", required = true)
        protected List<String> bar;

        ...
    }

With xs:seqence to get the list of Bars (maxOccurs="unbounded"): XSD:

    ...
    <xs:complexType name="Foo">
        <xs:sequence>
            <xs:element name="Bars" type="Bars" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Bars">
        <xs:sequence>
            <xs:element name="Bar" type="barType" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
    ...

Foo.java:

...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Foo", propOrder = {
    "bars"
})
public class Foo {

    @XmlElement(name = "Bars", required = true)
    protected List<Bars> bars;

    public List<Bars> getBars() {
        if (bars == null) {
            bars = new ArrayList<Bars>();
        }
        return this.bars;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...