将xsd转换为java时已定义的错误

时间:2014-09-06 01:52:33

标签: java xml ant jaxb xsd

我想将一组复杂的xsd文件翻译成Java / Hibernate实体。为此, 我下载了JAXB版本2.2.7 from this link。我解压缩文件,打开cmd.exe,导航到create-marshal示例的目录,运行ant compile以确认一切正常,然后运行ant clean以消除结果,将所有内容返回到启动条件。

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

  

[ERROR]' 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&#39;是[编译]
      完整的构建顺序是[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的构建ID是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] [ERROR]&#39; POCD_MT000040.InfrastructureRoot.typeId&#39;已经定义了       [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] [ERROR]&#39; POCD_MT000040.Act&#39;已经定义了       [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)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at 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>

1 个答案:

答案 0 :(得分:2)

您报告的错误表明,不知何故,已经指示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中的一个错误,由(无意)重复字段&#34; id&#34;在许多复杂类型中,例如:

<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调用。在蚂蚁中,这将是@binding的{​​{1}}:

<xjc>