如何使用JAXB生成的Java模型来获取所需的Java代码?

如何使用JAXB生成的Java模型来获取所需的Java代码?,java,xml,jaxb,xsd,Java,Xml,Jaxb,Xsd,这是我的xml模型: <train xmlns="http://www.example.org/train/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <routes> <route>Route1</route> <route>Route2</route> </routes> </train

这是我的xml模型:

<train xmlns="http://www.example.org/train/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <routes>
        <route>Route1</route>
        <route>Route2</route>
    </routes>
</train>
我尝试过不同的设计,如威尼斯百叶窗、俄罗斯洋娃娃、意大利腊肠片,但最终的结果总是这样:

Train train = new Train(); 
train.getRoutes().getRoute().add("Route1");
以下是我迄今为止尝试过的xsd文档:

百叶窗

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.org/train/">
  <xs:element xmlns:tra="http://www.example.org/train/" name="train" type="tra:trainType"/>
  <xs:complexType name="routesType">
    <xs:sequence>
      <xs:element type="xs:string" name="route" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="trainType">
    <xs:sequence>
      <xs:element xmlns:tra="http://www.example.org/train/" type="tra:routesType" name="routes"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

俄罗斯娃娃

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.org/train/">
  <xs:element name="train">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="routes">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="route" maxOccurs="unbounded" minOccurs="0"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

意大利腊肠片

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.org/train/">
  <xs:element name="route" type="xs:string"/>
  <xs:element name="routes">
    <xs:complexType>
      <xs:sequence>
        <xs:element xmlns:tra="http://www.example.org/train/" ref="tra:route" maxOccurs="unbounded" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="train">
    <xs:complexType>
      <xs:sequence>
        <xs:element xmlns:tra="http://www.example.org/train/" ref="tra:routes"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>


有人能告诉我我做错了什么吗?

您很可能需要
@xmlementwrapper
来实现类似的功能

@XmlElementWrapper(name="routes")
@XmlElement(name="route")
List<Route> routes ...;
@xmlementwrapper(name=“routes”)
@xmlement(name=“route”)
列出路线。。。;
为此,您可以使用

看看这个答案:


如果我从Java到XSD,元素包装器可能会工作,但我要从XSD到Java。请仔细阅读我的答案。jaxb xew插件是一个代码生成插件,完全适用于XSD->Java的情况,您是对的,该插件看起来可能是一个解决方案。有趣。
@XmlElementWrapper(name="routes")
@XmlElement(name="route")
List<Route> routes ...;