简体   繁体   English

使用来自 xsd 的 Jaxb 生成 java Array 而不是 Collection 类型

[英]Generate java Array instead of Collection type with Jaxb from xsd

我想知道是否有人已经使用 jaxb 的数据投标从 xsd 模式生成数组类型属性,而不是 List 类型,例如,不是生成 List ,而是生成 Employee []

In your XSD you would need to specify the javatype tag.在您的 XSD 中,您需要指定 javatype 标记。

<xs:complexType name="restaurant">
    <xs:sequence>
        <xs:element name="employee" type="employee" >
            <xs:annotation>
                <xs:appinfo>
                    <jxb:javaType name="Employee[]"/>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
</xs:complexType>

A simple example of how to do this would be if I was receiving a message for a new restaurant order I could create a new java class for mapping the XML Elements To various Java Types based on their field name.如何执行此操作的一个简单示例是,如果我收到一条关于新餐厅订单的消息,我可以创建一个新的 Java 类,用于根据字段名称将 XML 元素映射到各种 Java 类型。

  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(
    name = "restaurant",
    propOrder = {
     "name",
     "employeeArray"
   }
) 
public class RestaurantOrder {
   @XmlElement(name = "name")
   protected String name;

   @XmlElement(name = "employeeArray")
   protected Employee[] employeeArray;


   getter and setter for your employee array and name
}

Now once you have a JaxB Element you can do something similar to the following to retrieve your array By casting your JAXBElement to your newly created class.现在,一旦您拥有一个 JaxB 元素,您就可以通过将 JAXBElement 转换为新创建的类来执行类似于以下操作来检索您的数组。

public getArrayFromElement (JAXBElement<?> jaxbMessage) {
  RestaurantOrder order = (RestaurantOrder) jaxbMessage.getValue();
  return order.getItemArray();
}

Edit: This code assumes you have already setup a proper XSD and successfully unmarshalled your object (if you are having difficulty there then please clarify in your question).编辑:此代码假定您已经设置了适当的 XSD 并成功解组了您的对象(如果您在那里遇到困难,请在您的问题中澄清)。 The XSD for a class setup like this would requre you've established restaurant order as a complex type in your xsd and mapped those elements there properly as well to be able to successfully cast your jaxb message to the desired class.像这样的类设置的 XSD 需要您在 xsd 中将餐厅订单建立为复杂类型,并将这些元素正确映射到那里,以便能够成功地将 jaxb 消息转换为所需的类。 Just a heads up I used String[] if you had an employee object you could do Arraylist or w/e you want really.提醒一下,如果你有一个员工对象,你可以使用 Arraylist 或 w/e 你真正想要的对象,我使用 String[]。

As a further note you can accept multiple complex types using JAXB by creating this kind of relationship.进一步说明,您可以通过创建这种关系来使用 JAXB 接受多种复杂类型。 You can create a command pattern to parse and cast unmarshalled JAXB elements to ANY class you have mapped and this will allow you to manipulate the data to your liking while providing an extendable and scalable way to reuse your JAXB Parser that will be easy to maintain and update as needed.您可以创建一个命令模式来解析解组的 JAXB 元素并将其转换为您映射的任何类,这将允许您根据自己的喜好操作数据,同时提供一种可扩展和可扩展的方式来重用您的 JAXB 解析器,这将易于维护和根据需要更新。

In jaxb:globalBindings use the atribute collectionType="indexed" .jaxb:globalBindings使用属性collectionType="indexed" More info here " Customizing JAXB Bindings "此处的更多信息“ 自定义 JAXB 绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM