JAXB类型问题 [英] JAXB Types problem

查看:79
本文介绍了JAXB类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的xsd(片段):

I have an xsd that looks like this (snippet):

<xs:complexType name="IDType">
  <xs:choice minOccurs="1" maxOccurs="2">
    <xs:element name="FileID"    minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="IDNumber1" minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="Number"    minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="PNumber"   minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="SS"        minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="Player"    minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="Prior"     minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="BIN"       minOccurs="0" maxOccurs="1" type="an..35" />
    <xs:element name="Mutual"    minOccurs="0" maxOccurs="1" type="an..35" />
  </xs:choice>
</xs:complexType>
<xs:simpleType name="an..35">
  <xs:restriction base="an">
    <xs:maxLength value="35" />
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="an">
   <xs:restriction base="xs:string">
     <xs:pattern value="[ !-~]*" />
   </xs:restriction>
</xs:simpleType>

由于某种原因,这是生成的Java代码:

For some reason this is the Java code that gets generated:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IDType", propOrder = {
    "fileID"
})
public class PatientIDType {
    @XmlElementRefs({
        @XmlElementRef(name = "FileED", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "IDNumber1", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "Number", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "PNumber", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "SS", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "Plaer", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "Prior", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "BIN", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class),
        @XmlElementRef(name = "Mutual", namespace = "http://www.surescripts.com/messaging", type = JAXBElement.class)
    })
    protected List<JAXBElement<String>> fileID;
    /**
     * Gets the value of the fileID property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the fileID property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getFileID().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     */
    public List<JAXBElement<String>> getFileID() {
        if (fileID == null) {
            fileID = new ArrayList<JAXBElement<String>>();
        }
        return this.fileID;
    }

为什么这样生成的类就是这样而不是某种字符串数组?我真的不想每次想要创建东西时都要创建JAXBElements吗?

Why is the class generated like this and simply not some kind of string array? I really don't want to have to create JAXBElements every time I want to create something?

如何让它为每个只代表字符串的类型生成类或类似的东西?

How can I have it generate classes for each of the types that simply represent string or something like that?

提前致谢,

Ian

推荐答案

生成此代码是因为您的复杂类型IDType包含maxOccurrence大于1的选项,此处:

This code is generated because your complex type, IDType, contains a choice with a maxOccurrence greater than one, here:

< xs:choice minOccurs =1maxOccurs =2>

<xs:choice minOccurs="1" maxOccurs="2">

此列表的内容是具有不同名称的元素但是相同的类型。这在标准的面向对象模型中没有等价物。然后JAXB使用 JAXBElement 类来解决这个问题: JAXBElement 包装一个包含数据并分配数据的简单对象a QName

The contents of this lists are elements with different names but the same type. This has no equivalent in standard object-oriented models. JAXB then uses the JAXBElement class to work around this: a JAXBElement wraps a simple object that contains the data and assigns it a QName.

因此,您可以从此列表中读取并通过提供以下内容明确地写入列表:

Thus you can read from this list and write to the list unambiguously by providing:


  • 数据对象(在您的情况下为String,因为String的所有限制都表示为Java字符串)

  • JAXBElement getValue()方法返回字符串

  • The data object (in your case a String since all restrictions from String are represented as a Java String)
  • A JAXBElement whose getValue() method returns the string

JAXB规范包含了如何处理重复选择和重复序列的相当详细和相当复杂的解释。请注意,如果您的序列包含不同类型的对象以及不同的名称,您最终会得到 List< Object>

The JAXB specification contains fairly detailed and reasonably complex explanations of how to deal with repeating choices and repeating sequences. Note that if your sequence contained objects of different types as well as different names, you would end up with List<Object>.

那是很长的解释,现在这里有一些选项:

That was the long explanation, now here are some options:


  • 如果你可以修改架构,在8个项目周围放一个包装元素,比方说, 包装。包装器将包含单个元素的选择;然后使IDType包含一个包含minOccurs = 1和maxOccurs = 2的Wrapper元素序列。

  • 创建一些帮助函数以快速创建JAXBElements。 JAXB在您的目标包中放置了一个可以帮助您的Factory类 - 例如,它包含对Schema命名空间的引用等。

这篇关于JAXB类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆