我想将一组复杂的xsd文件转换为Java / Hibernate实体。为此,
我下载了JAXB from this link的2.2.7版本。我解压缩文件,打开cmd.exe,导航到create-marshal示例的目录,运行ant编译以确认一切正常,然后运行ant clean消除结果以使一切恢复到初始状态。

当我尝试使用其他xsd文件作为create-marshal示例的输入时,问题就来了。具体来说,当xsd文件通过更改相同标记类型的属性的值来区分数据结构时,我得到了一个错误。在这种情况下,create-marshal示例给出以下错误:

[错误]'POCD_MT000040.InfrastructureRoot.typeId'已定义
[xjc]文件的第54行:/ C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd

错误一次又一次地重复,每次为不同的数据结构定义重新分配属性的值时。 如何解决此错误?

这是xsd文件中数据结构的前几次迭代:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema targetNamespace="urn:the-publisher:v3"
  xmlns:mif="urn:the-publisher:v3/mif"
  xmlns="urn:the-publisher:v3"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  xmlns:sdtc="urn:the-publisher:sdtc">
<xs:annotation>
    <xs:documentation>Manually edited to add Schema Extensions, July 2012</xs:documentation>
    <xs:documentation>Generated using schema builder version 2.0. Stylesheets: RoseTreeToMIFStaticModel.xsl version: 1.1 StaticMifToXsd.xsl version 2.0</xs:documentation>
</xs:annotation>
<xs:complexType name="POCD_MT000040.InfrastructureRoot.typeId">
    <xs:complexContent>
        <xs:restriction base="II">
            <xs:attribute name="root" type="uid"
              use="required"
              fixed="2.16.840.1.113883.1.3"/>
            <xs:attribute name="extension" type="st"
              use="required"/>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="POCD_MT000040.Act">
    <xs:sequence>
        <xs:element name="realmCode" type="CS"
          minOccurs="0"
          maxOccurs="unbounded"/>
        <xs:element name="typeId"
          type="POCD_MT000040.InfrastructureRoot.typeId"
          minOccurs="0"/>
        <xs:element name="templateId" type="II"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="id" type="II" minOccurs="0"
          maxOccurs="unbounded"/>
        <xs:element name="code" type="CD"/>
        <xs:element name="text" type="ED" minOccurs="0"/>
        <xs:element name="statusCode" type="CS"
          minOccurs="0"/>
        <xs:element name="effectiveTime" type="IVL_TS"
          minOccurs="0"/>
        <xs:element name="priorityCode" type="CE"
          minOccurs="0"/>
        <xs:element name="languageCode" type="CS"
          minOccurs="0"/>
        <xs:element name="subject"
          type="POCD_MT000040.Subject" minOccurs="0"/>
        <xs:element name="specimen"
          type="POCD_MT000040.Specimen" minOccurs="0"
          maxOccurs="unbounded"/>
        <xs:element name="performer"
          type="POCD_MT000040.Performer2" minOccurs="0"
          maxOccurs="unbounded"/>
        <xs:element name="author"
          type="POCD_MT000040.Author" minOccurs="0"
          maxOccurs="unbounded"/>
        <xs:element name="informant"
          type="POCD_MT000040.Informant12"
          minOccurs="0"
          maxOccurs="unbounded"/>
        <xs:element name="participant"
          type="POCD_MT000040.Participant2"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="entryRelationship"
          type="POCD_MT000040.EntryRelationship"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="reference"
          type="POCD_MT000040.Reference"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="precondition"
          type="POCD_MT000040.Precondition"
          minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="nullFlavor"
          type="NullFlavor" use="optional"/>
    <xs:attribute name="classCode"
          type="x_ActClassDocumentEntryAct" use="required"/>
    <xs:attribute name="moodCode" type="x_DocumentActMood"
          use="required"/>
    <xs:attribute name="negationInd" type="bl"
          use="optional"/>
</xs:complexType>
<xs:complexType name="POCD_MT000040.AssignedAuthor">
    <xs:sequence>
        <xs:element name="realmCode" type="CS"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="typeId"
          type="POCD_MT000040.InfrastructureRoot.typeId"
          minOccurs="0"/>
        <xs:element name="templateId" type="II"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="id" type="II"
          maxOccurs="unbounded"/>
        <xs:element name="code" type="CE" minOccurs="0"/>
        <xs:element name="addr" type="AD"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="telecom" type="TEL"
          minOccurs="0" maxOccurs="unbounded"/>
        <xs:choice>
            <xs:element name="assignedPerson"
          type="POCD_MT000040.Person" minOccurs="0"/>
            <xs:element name="assignedAuthoringDevice"
          type="POCD_MT000040.AuthoringDevice"
          minOccurs="0"/>
        </xs:choice>
        <xs:element name="representedOrganization"
          type="POCD_MT000040.Organization" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="nullFlavor"
          type="NullFlavor" use="optional"/>
    <xs:attribute name="classCode"
          type="RoleClassAssignedEntity"
          use="optional" fixed="ASSIGNED"/>
</xs:complexType>

这是错误消息的相应的前几次迭代:

目标“编译”的构建顺序为[编译]
完整的构建顺序是[compile,javadoc,clean,run,]
编译:
[echo]编译架构...
[mkdir]创建的目录:C:\ Temp \ jaxb \ apps \ create-marshal \ gen-src
[mkdir]创建的目录:C:\ Temp \ jaxb \ apps \ create-marshal \ gen-src \ primer \ po
[xjc] XJC的内部版本号是2.2.7
[xjc]检查C:\ Temp \ jaxb \ apps \ create-marshal \ POCD_MT000040_SDTC.xsd的时间戳
[xjc]输入的最后修改时间为1409957672397
[xjc]输出的最后修改时间是-9223372036854775808
[xjc]编译文件:/ C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
[xjc] [错误]'POCD_MT000040.InfrastructureRoot.typeId'已定义
[xjc]文件的第54行:/ C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
[xjc]
[xjc] [错误](与上述错误有关),第一个定义出现在此处
[xjc]文件的第46行:/ C:/Temp/jaxb/apps/create-marshal/infrastructure/cda/POCD_MT000040_SDTC.xsd
[xjc]
[xjc] [错误]'POCD_MT000040.Act'已定义
[xjc]文件的第81行:/ C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
[xjc]

.....同一错误重复多次,每次xs:complexType重复一次

[xjc] XJC任务失败。使用Ant -verbose开关了解更多详细信息
编译:持续时间2秒
建立失败
C:\ Temp \ jaxb \ apps \ create-marshal \ build.xml:29:无法解析架构。应该提供错误消息
在com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:520)
在com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:457)
在com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:380)
在com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:103)
在org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:606)
在org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
在org.apache.tools.ant.Task.perform(Task.java:348)
在org.apache.tools.ant.Target.execute(Target.java:435)
在org.apache.tools.ant.Target.performTasks(Target.java:456)
在org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
在org.apache.tools.ant.Project.executeTarget(Project.java:1364)
在org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
在org.apache.tools.ant.Project.executeTargets(Project.java:1248)
在org.apache.tools.ant.Main.runBuild(Main.java:851)
在org.apache.tools.ant.Main.startAnt(Main.java:235)
在org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
在org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

这是ant构建文件:
<?xml version="1.0" standalone="yes"?>
<!--  Copyright 2004 Sun Microsystems, Inc. All rights reserved. -->
<project basedir="." default="run">
<description>This sample application demonstrates how to use the ObjectFactory class to create a Java content tree from scratch and marshal it to XML data. It also demonstrates how to add content to a JAXB List property.</description>
<record name="build.log" loglevel="verbose" action="start"/>
<property name="jaxb.home" value="../.." />
<path id="classpath">
    <pathelement path="src" />
    <pathelement path="classes" />
    <pathelement path="schemas" />
    <fileset dir="${jaxb.home}" includes="lib/*.jar" />
</path>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
    <classpath refid="classpath" />
</taskdef>
<!--compile Java source files-->
<target name="compile" description="Compile all Java source files">
    <echo message="Compiling the schema..." />
    <mkdir dir="gen-src" />
    <mkdir dir="gen-src/primer/po" />
    <xjc schema="POCD_MT000040_SDTC.xsd" package="primer.po" destdir="gen-src">
        <produces dir="gen-src/primer/po" includes="**/*.java" />
    </xjc>
    <echo message="Compiling the java source files..." />
    <mkdir dir="classes" />
    <javac destdir="classes" debug="on">
      <src path="src" />
      <src path="gen-src" />
      <classpath refid="classpath" />
    </javac>
  </target>
  <target name="run" depends="compile" description="Run the sample app">
    <echo message="Running the sample application..." />
    <java classname="Main" fork="true">
      <classpath refid="classpath" />
    </java>
  </target>
  <target name="javadoc" description="Generates javadoc" depends="compile">
    <echo message="Generating javadoc..." />
    <mkdir dir="docs/api" />
    <javadoc sourcepath="gen-src" destdir="docs/api" windowtitle="create-marshal (formerly SampleApp3)" useexternalfile="yes">
      <fileset dir="." includes="gen-src/**/*.java" excludes="**/impl/**/*.java" />
    </javadoc>
  </target>
  <target name="clean" description="Deletes all the generated artifacts.">
    <delete dir="docs/api" />
    <delete dir="gen-src" />
    <delete dir="schemas" />
    <delete dir="classes" />
  </target>
</project>

最佳答案

您报告的错误表明,某种程度上,已指示ant两次读取相同的XML Schema定义。确实,错误消息说一个定义在

C:\Temp\jaxb\apps\create-marshal\POCD_MT000040_SDTC.xsd

另一个在
...\create-marshal\infrastructure\cda\POCD_MT000040_SDTC.xsd

您应该清理create-marshal下面的文件树。您需要这两个文件之一,并且相对于此文件,它需要包含的文件,例如:
..\coreschemas\datatypes.xsd
..\coreschemas\voc.xsd
..\coreschemas\NarrativeBlock.xsd

最后,datatypes-base.xsd必须与这三个文件夹位于同一文件夹中,因为datatypes.xsd包含了该文件夹。

但是,如果您对此进行了整理,则会遇到另一个问题,至少对于JDK 1.8随附的JAXB而言。这是xjc中的一个错误,是由(复杂的)字段“id”以多种复杂类型(例如:
<xs:complexType name="POCD_MT000040.ObservationMedia">
    ...
    <xs:element name="id" type="II" minOccurs="0" maxOccurs="unbounded"/>
    ...
  <xs:attribute name="ID" type="xs:ID"/>

XML架构名称到Java名称的映射是在JAXB规范中定义的,并且,无论如何产生冲突,这都将导致错误消息(发生时不是堆栈转储)。

您必须通过使用JAXB绑定文件进行自定义来解决此问题(错误或没有错误)。这是一个(我们称它为rename.xjb),将与POCD_MT000040.ObservationMedia中匹配@ID的字段重命名为`xsid

修复所有名称清除ID与ID
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      version="2.1">

<bindings schemaLocation="./POCD_MT000040.xsd" node="/xs:schema">
    <bindings node="//xs:complexType[@name='POCD_MT000040.ObservationMedia']/xs:attribute[@name='ID']">
        <property name="xsid"/>
    </bindings>
</bindings>

<bindings schemaLocation="./POCD_MT000040.xsd" node="/xs:schema">
    <bindings node="//xs:complexType[@name='POCD_MT000040.RegionOfInterest']/xs:attribute[@name='ID']">
        <property name="xsid"/>
    </bindings>
</bindings>

<bindings schemaLocation="./POCD_MT000040.xsd" node="/xs:schema">
    <bindings node="//xs:complexType[@name='POCD_MT000040.Section']/xs:attribute[@name='ID']">
        <property name="xsid"/>
    </bindings>
</bindings>
</bindings>

您必须将绑定文件添加到xjc调用中。在ant中,这将是@binding<xjc>:
<xjc schema="POCD_MT000040_SDTC.xsd" binding="rename.xjb" ...

10-06 14:49