Java 如何为 XSD 中的基本类型生成 @XmlRootElement 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1514110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:41:57  来源:igfitidea点击:

How to generate @XmlRootElement Classes for Base Types in XSD?

javaxsdjaxbxjc

提问by jallch

I am having some issues with generating Java Classes with appropriate JAXB annotations from an XSD using XJC.

我在使用 XJC 从 XSD 生成带有适当 JAXB 注释的 Java 类时遇到了一些问题。

I have a relatively simple XSD file defining my XML schema. The complex types within the XSD take advantage of inheritance using the <xs:extension>tags. The problem I having is that I need all complex types to generate Java Classes with the @XmlRootElement.

我有一个相对简单的 XSD 文件来定义我的 XML 模式。XSD 中的复杂类型利用<xs:extension>标签继承。我遇到的问题是我需要所有复杂类型来生成带有@XmlRootElement.

Unfortunately, the way in which XJC generates the classes means that only derived class gets the @XmlRootElement(not the base class). I am using the simple global binding directive to ensure that it solves many of the other issues that I have faced with XJC.

不幸的是,XJC 生成类的方式意味着只有派生类得到@XmlRootElement(而不是基类)。我正在使用简单的全局绑定指令来确保它解决了我在 XJC 中遇到的许多其他问题。

Here is an example snippet of the XSD:

这是 XSD 的示例片段:

<xs:schema version="1.0" targetNamespace="http://www.knowledgemill.com/kmcs"
  xmlns:kmcs="http://www.knowledgemill.com/kmcs"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  jaxb:version="2.0"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  jaxb:extensionBindingPrefixes="xjc"
  elementFormDefault="qualified">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <xjc:simple />
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="Artifact" type="kmcs:Artifact"/>
    <xs:element name="EmailArtifact" type="kmcs:EmailArtifact"/>

    <xs:complexType name="Artifact">
        <xs:sequence>
            <xs:element name="artifactId" type="xs:string" minOccurs="0"/>
            <xs:element name="artifactType" type="xs:string" minOccurs="0"/>
            <xs:element name="contentHash" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="EmailArtifact">
        <xs:complexContent>
            <xs:extension base="kmcs:Artifact">
                <xs:sequence>
                    <xs:element name="subject" type="xs:string" minOccurs="0"/>
                    <xs:element name="threadSubject" type="xs:string" minOccurs="0"/>
                    <xs:element name="from" type="xs:string" minOccurs="0"/>
                    <xs:element name="to" type="xs:string" minOccurs="0"/>
                    <xs:element name="cc" type="xs:string" minOccurs="0"/>
                    <xs:element name="bcc" type="xs:string" minOccurs="0"/>
                    <xs:element name="messageId" type="xs:string" minOccurs="0"/>
                    <xs:element name="date" type="xs:date" minOccurs="0"/>
                    <xs:element name="size" type="xs:long" minOccurs="0"/>
                    <xs:element name="hasAttachment" type="xs:boolean" minOccurs="0"/>
                    <xs:element name="sensitivity" type="xs:string" minOccurs="0"/>
                    <xs:element name="headerHash" type="xs:string" minOccurs="0"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>


</xs:schema>

As we can see from the above snippet, EmailArtifactextends Artifact.

正如我们从上面的代码片段中看到的,EmailArtifactextends Artifact

The java class code for EmailArtifactcontains the following:

的 java 类代码EmailArtifact包含以下内容:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "EmailArtifact", propOrder = {
    "subject",
    "threadSubject",
    "from",
    "to",
    "cc",
    "bcc",
    "messageId",
    "date",
    "size",
    "hasAttachment",
    "sensitivity",
    "headerHash"
})
@XmlSeeAlso({
    ExtendedEmail.class
})
@XmlRootElement(name = "EmailArtifact")
public class EmailArtifact
    extends Artifact
{

    protected String subject;
    protected String threadSubject;
    protected String from;
    protected String to;
    protected String cc;
    protected String bcc;
    protected String messageId;
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar date;
    protected Long size;
    protected Boolean hasAttachment;
    protected String sensitivity;
    protected String headerHash;

The java class code for Artifactcontains the following:

的 java 类代码Artifact包含以下内容:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Artifact", propOrder = {
    "artifactId",
    "artifactType",
    "contentHash"
})
@XmlSeeAlso({
    ManagedDocArtifact.class,
    EmailArtifact.class
})
public class Artifact {

    protected String artifactId;
    protected String artifactType;
    protected String contentHash;

In the EmailArtifactwe can see that it contains the @XmlRootElementbut the base type Artifactdoes not contain @XmlRootElement.

在 中EmailArtifact我们可以看到它包含 ,@XmlRootElement但基本类型Artifact不包含@XmlRootElement

How can you force XJC to generate @XmlRootElementfor all classes including the base types.

如何强制 XJC 为@XmlRootElement包括基本类型在内的所有类生成。

回答by DavidValeri

This questionreferences a blogpost by Kohsuke Kawaguchi, formerly worked on the JAX-B or JAX-WS RI, that talks about the RI's decision making process and lack of clarity on this issue in the spec.

这个问题引用了Kohsuke Kawaguchi的一篇博文,他之前在 JAX-B 或 JAX-WS RI 工作过,其中谈到了 RI 的决策过程以及规范中对这个问题的缺乏明确性。

The blog post mentions that the simple binding feature is part of the RI. Are you using the RI to generate your code?

博客文章提到简单绑定功能是 RI 的一部分。您是否使用 RI 生成代码?

回答by Azee

Just bind using xjb-file:

只需使用 xjb-file 绑定:

<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <jxb:bindings schemaLocation="path/to/myschema.xsd" node="/xs:schema">
    <jxb:globalBindings>
      <xjc:simple />
    </jxb:globalBindings>
  </jxb:bindings>
</jxb:bindings>

And don't forget to define element of the same type:

并且不要忘记定义相同类型的元素:

<xs:complexType name="Artifact" />
<xs:element name="artifact" type="Artifact">