创建内部类的JAXB列表标记

时间:2022-11-19 15:12:07

So we have an XSD type in the form:

所以我们在表单中有一个XSD类型:

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

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

xjc生成几乎正确的结果。唯一令人讨厌的是,“Bars”是作为一个内部类创建的,它存储了一个bar列表。无论如何,在Foo中有一个列表,同时仍然保留上面的XML吗?

5 个解决方案

#1


2  

Another way would be to drop the surrounding <bars> element, the XML does not look so nice anymore, but it would make the java code easier to read. Together with xjc's simple binding (see http://jaxb.java.net/nonav/jaxb20-fcs/docs/vendorCustomizations.html) it will produce quite pretty and usefull java code.

另一种方法是删除周围的 元素,XML看起来不再那么好了,但它将使java代码更容易阅读。与xjc的简单绑定一起(参见http://jaxb.java.net/nonav/jaxb20-fcs/docs/vendorCustomizations.html),它将生成相当漂亮和有用的java代码。

#2


6  

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.

当您将bar定义为复杂类型时,bar将被生成为独立的类。像这样,我发现模式也更容易阅读。在Foo中,除非将maxOccurs更改为大于1的值,否则将不会列出bar -您不能在xs:all上执行此操作,但是您可以使用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:

跑后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:

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:seqence获取条的列表(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:

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;
    }
}

#3


4  

Bjarne Hansen's plugin for xjc (https://github.com/dmak/jaxb-xew-plugin) would allow you to stay with "Bars" enclosing element, generating convenient @XmlElementWrapper(name="Bars") annotation.

Bjarne Hansen的xjc插件(https://github.com/dmak/jaxb-xew-plugin)允许您使用“bar”封装元素,生成方便的@XmlElementWrapper(name="Bars")注释。

#4


2  

Maybe below sample helps.

也许下面的示例有帮助。

XML Schema

XML模式

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

      <xs:complexType name="test">
        <xs:sequence>
          <xs:element name="dataList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
          <xs:element name="str" type="xs:string" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>

Java class :

Java类:

public class Test {

    protected List<String> dataList;
    protected String str;
    public List<String> getDataList() {
        if (dataList == null) {
            dataList = new ArrayList<String>();
        }
        return this.dataList;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String value) {
        this.str = value;
    }

}

EDIT 1:

编辑1:

You can reverse engineer from java code to xsd. in JAVA_HOME/bin there is schemagen executable.

您可以将工程从java代码反向到xsd。在JAVA_HOME/bin中有schemagen可执行文件。

Give your java code and it will create the XSD schema for you using your java class.

给出java代码,它将使用java类为您创建XSD模式。

see this link

看到这个链接

schemagen myObj1.java myObj2.java

#5


1  

You could do the following:

你可以这样做:

package example;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Foo")
public class Foo {

    private List<Bar> bar;

    public List<Bar> getBar() {
        return bar;
    }

    @XmlElementWrapper(name="Bars")
    @XmlElement(name="Bar")
    public void setBar(List<Bar> bar) {
        this.bar = bar;
    }

}

and

package example;

public class Bar {

}

Then you can process your XML using the following code;

然后可以使用以下代码处理XML;

package example;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo foo = (Foo) unmarshaller.unmarshal(new File("src/forum128/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }
}

#1


2  

Another way would be to drop the surrounding <bars> element, the XML does not look so nice anymore, but it would make the java code easier to read. Together with xjc's simple binding (see http://jaxb.java.net/nonav/jaxb20-fcs/docs/vendorCustomizations.html) it will produce quite pretty and usefull java code.

另一种方法是删除周围的 元素,XML看起来不再那么好了,但它将使java代码更容易阅读。与xjc的简单绑定一起(参见http://jaxb.java.net/nonav/jaxb20-fcs/docs/vendorCustomizations.html),它将生成相当漂亮和有用的java代码。

#2


6  

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.

当您将bar定义为复杂类型时,bar将被生成为独立的类。像这样,我发现模式也更容易阅读。在Foo中,除非将maxOccurs更改为大于1的值,否则将不会列出bar -您不能在xs:all上执行此操作,但是您可以使用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:

跑后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:

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:seqence获取条的列表(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:

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;
    }
}

#3


4  

Bjarne Hansen's plugin for xjc (https://github.com/dmak/jaxb-xew-plugin) would allow you to stay with "Bars" enclosing element, generating convenient @XmlElementWrapper(name="Bars") annotation.

Bjarne Hansen的xjc插件(https://github.com/dmak/jaxb-xew-plugin)允许您使用“bar”封装元素,生成方便的@XmlElementWrapper(name="Bars")注释。

#4


2  

Maybe below sample helps.

也许下面的示例有帮助。

XML Schema

XML模式

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

      <xs:complexType name="test">
        <xs:sequence>
          <xs:element name="dataList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
          <xs:element name="str" type="xs:string" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>

Java class :

Java类:

public class Test {

    protected List<String> dataList;
    protected String str;
    public List<String> getDataList() {
        if (dataList == null) {
            dataList = new ArrayList<String>();
        }
        return this.dataList;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String value) {
        this.str = value;
    }

}

EDIT 1:

编辑1:

You can reverse engineer from java code to xsd. in JAVA_HOME/bin there is schemagen executable.

您可以将工程从java代码反向到xsd。在JAVA_HOME/bin中有schemagen可执行文件。

Give your java code and it will create the XSD schema for you using your java class.

给出java代码,它将使用java类为您创建XSD模式。

see this link

看到这个链接

schemagen myObj1.java myObj2.java

#5


1  

You could do the following:

你可以这样做:

package example;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Foo")
public class Foo {

    private List<Bar> bar;

    public List<Bar> getBar() {
        return bar;
    }

    @XmlElementWrapper(name="Bars")
    @XmlElement(name="Bar")
    public void setBar(List<Bar> bar) {
        this.bar = bar;
    }

}

and

package example;

public class Bar {

}

Then you can process your XML using the following code;

然后可以使用以下代码处理XML;

package example;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo foo = (Foo) unmarshaller.unmarshal(new File("src/forum128/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }
}