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



我想将一组复杂的xsd文件转换为Java/HHibernate实体。为此,我从这个链接下载了JAXB的2.2.7版本。我解压缩了文件,打开cmd.exe,导航到创建封送示例的目录,运行ant compile确认一切正常,然后运行ant clean消除结果,将一切返回到启动条件。

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

[错误]"POCD_MT000040.InfrastructureRoot.typeId"已定义
文件的[xjc]第54行:/C:/Temp/jaxb/apps/create封送/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"的生成顺序为[compile]
完整的构建顺序是[compile,javadoc,clean,run,]
编译:
[echo]正在编译架构
[mkdir]创建的目录:C:\Temp\jaxb\apps\create marshall\gen src
[mkdir]创建的目录:C:\Temp\jaxb\apps\create marshall\gen src\primer\po
xjc的[xjc]构建id为2.2.7
[xjc]正在检查C:\Temp\jaxb\apps\create marshall\POCD_MT000040_SDTC.xsd的时间戳
[xjc]输入的最后修改时间是14099557672397
[xjc]输出的最后修改时间是9223372036854775808
[xjc]编译文件:/C:/Temp/jaxb/apps/create marshall/POCD_MT000040_SDTC.xsd
[xjc][错误]"POCD_MT000040.InfrastructureRoot.typeId"已定义
文件的[xjc]第54行:/C:/Temp/jaxb/apps/create marshall/POCD_MT000040_SDTC.xsd
[xjc]
[xjc][ERROR](与上述错误相关)第一个定义出现在此处
文件的[xjc]第46行:/C:/Temp/jaxb/apps/create marshall/infrastructure/cda/POCD_MT000040_SDTC.xsd
[xjc]
[xjc][ERROR]'POCD_MT000040.Act'已定义
文件的[xjc]第81行:/C:/Temp/jaxb/apps/create marshall/POCD_MT000040_SDTC.xsd
[xjc]

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

[xjc]在xjc任务中失败。有关详细信息,请使用Ant-verbose开关
编译:持续时间2秒
构建失败
C: \Temp\jaxb\apps\create marshall\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.exexecute(XJC2Task.java:380)
网址:com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:103)
网址:org.apache.tools.ant.UnnknownElement.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.ioke(Method.java:606)
网址:org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtil.java:106)
网址:org.apache.tools.ant.Task.aperform(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.slaunch.Launcher.run(Launcher.java:280)
网址:org.apache.tools.ant.slaunch.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被指示读取相同的XMLSchema定义两次。事实上,错误消息说中有一个定义

C:Tempjaxbappscreate-marshalPOCD_MT000040_SDTC.xsd

另一个是在中发现的

...create-marshalinfrastructurecdaPOCD_MT000040_SDTC.xsd

您应该清理create marshall下面的文件树。您需要这两个文件中的一个,并且与该文件相关,它包含的文件,例如:

..coreschemasdatatypes.xsd
..coreschemasvoc.xsd
..coreschemasNarrativeBlock.xsd

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

但是,当您清理完这个问题后,您将遇到另一个问题,至少JDK1.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绑定文件的自定义来处理这个问题(bug或无bug)。这里有一个(让我们称之为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调用中。在蚂蚁中,这将是<xjc>:的@binding

<xjc schema="POCD_MT000040_SDTC.xsd" binding="rename.xjb" ...

相关内容

  • 没有找到相关文章