1

我正在尝试使用来自 jaxb2_commons maven 组的 jaxb2-basics 工件添加接口。

我的 pom.xml 包含以下依赖项

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.2.7</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.2.6</version>
</dependency>
<dependency>
  <groupId>org.jvnet.jaxb2_commons</groupId>
  <artifactId>jaxb2-basics-runtime</artifactId>
  <version>0.6.4</version>
</dependency>

插件配置看起来像

  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb22-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
      <execution>
        <id>jaxb-generate-messages-in</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <specVersion>2.2</specVersion>
          <schemaLanguage>XMLSCHEMA</schemaLanguage>
          <schemaDirectory>src/main/schema</schemaDirectory>
          <schemaIncludes>
            <include>MESSAGES-IN.xsd</include>
          </schemaIncludes>
          <bindingDirectory>src/main/binding</bindingDirectory>
          <bindingIncludes>
            <include>messages-in-binding.xjb</include>
          </bindingIncludes>
          <episodeFile>${project.build.directory}/generated-sources/messages-in/META-INF/jaxb-messages-in.episode</episodeFile>
          <generateDirectory>${project.build.directory}/generated-sources/messages-in</generateDirectory>
          <extension>true</extension>
          <args>
            <arg>-Xinheritance</arg>
          </args>
          <plugins>
            <plugin>
              <groupId>org.jvnet.jaxb2_commons</groupId>
              <artifactId>jaxb2-basics</artifactId>
              <version>0.6.4</version>
            </plugin>
          </plugins>
        </configuration>
      </execution>
      <execution>
        <id>jaxb-generate-messages-out</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <specVersion>2.2</specVersion>
          <schemaLanguage>XMLSCHEMA</schemaLanguage>
          <schemaDirectory>src/main/schema</schemaDirectory>
          <schemaIncludes>
            <include>MESSAGES-OUT.xsd</include>
          </schemaIncludes>
          <bindingDirectory>src/main/binding</bindingDirectory>
          <bindingIncludes>
            <include>messages-out-binding.xjb</include>
          </bindingIncludes>
          <episodeFile>${project.build.directory}/generated-sources/messages-out/META-INF/jaxb-messages-out.episode</episodeFile>
          <generateDirectory>${project.build.directory}/generated-sources/messages-out</generateDirectory>
        </configuration>
      </execution>
    </executions>
    <configuration>
      <verbose>true</verbose>
    </configuration>
  </plugin>

从上面可以看出,xjc 有两次调用,它们都有效。专注于第一个,我的绑定文件

<jaxb:bindings
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jaxb:extensionBindingPrefixes="xjc"
  version="1.0">
  <jaxb:bindings schemaLocation="../schema/MESSAGES-IN.xsd" node="/xs:schema">
    <jaxb:globalBindings typesafeEnumMaxMembers="3000">
      <jaxb:serializable uid="1"/>
    </jaxb:globalBindings>
    <jaxb:schemaBindings>
      <jaxb:package name="com.whatever.messages.request"/>
    </jaxb:schemaBindings>
    <jaxb:bindings node="//xs:simpleType[@name='YesOrNo']">
      <jaxb:class ref="com.whatever.messages.YesOrNo"/>
    </jaxb:bindings>
    <jaxb:bindings noade="//xs:complexType[@name='someLogin']">
      <jaxb:class name="LoginRequest">
        <jaxb:javadoc><![CDATA[A Login request message.]]>
        </jaxb:javadoc>
      </jaxb:class>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

奇迹般有效; 但是,当我尝试向“LoginRequest”添加接口时......

   <jaxb:bindings node="//xs:complexType[@name='someLogin']">
     <jaxb:class name="LoginRequest">
       <jaxb:javadoc><![CDATA[A Login request message.]]>
       </jaxb:javadoc>
     </jaxb:class>
     <inheritance:implements>com.whatever.messages.Request</inheritance:implements>
   </jaxb:bindings>

我收到错误消息

 Error while parsing schema(s).Location [ file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/binding/messages-in-binding.xjb{19,42}].
 com.sun.istack.SAXParseException2; systemId: file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/binding/messsages-in-binding.xjb; lineNumber: 19; columnNumber: 42; compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.

报告该位置是

 Error while generating code.Location [ file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/schema/MESSAGES-IN.xsd{106693,54}].

恰好对应于

 <xs:complexType name="someLogin" mixed="true">
   ...
 </xs:complexType>

现在,我尝试了第二个指令将接口绑定到 XSD 元素

 <xs:element name="someLogin" type="someLogin" substitutionGroup="externalMethod"/>

但我只是收到与元素的行号相同的错误消息作为位置。

显然,有人想将接口附加到类,并且所有示例看起来都非常接近我的绑定文件,但一定有问题。

我的环境是

Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)
Maven home: C:\Program Files\NetBeans 7.2.1\java\maven
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: C:\Program Files (x86)\Java\jdk1.7.0_07\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

有人可以解释为什么 xjc 认为扩展在错误的 XSD 类型上运行吗?

4

1 回答 1

0

在施工

<jaxb:bindings 
  node="//Xs:complexType[@name='someLogin">
  ...
</jaxb:bindings>

node属性应该是一个 XPath 表达式。(或者它在Oracle上是这么说的)。尝试添加右方括号使其成为一个,看看是否有帮助。

于 2012-12-08T00:10:41.037 回答