3

我有一个架构,JAXB每次都能完美地生成 java 类。我正在尝试hyperjaxb处理相同的架构。为此,我从该链接hyperjaxb下载并解压缩了maven 项目,然后使用导航到根目录并使用 示例数据对其进行测试,以确保它与示例模式一起使用。然后,我用在 中完美运行的完整 shema替换了、和文件,包括许多导入的命名空间。然后我又跑了。但是,我收到以下错误消息:cmd.exemvn clean installschema.xsdpo.xmlbindings.xjbJAXBmvn clean install

[ERROR] Error while parsing schema(s).Location [ file:/C:/path/to/src/main/resources/schema.xsd{4,32}].
org.xml.sax.SAXParseException; systemId: file:/C:/path/to/src/main/resources/schema.xsd; 
lineNumber: 4; columnNumber: 32; Unsupported binding namespace "". 
Perhaps you meant "http://annox.dev.java.net"?

在示例中,schema.xsd 顶部的模式标记是:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">

相比之下,我的自定义架构使用:

<xs:schema targetNamespace="urn:some-org:v3" xmlns:mif="urn:some-org:v3/mif" xmlns="urn:some-org:v3"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:sdtc="urn:some-org:sdtc" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">  

请注意xmlns="urn:some-org:v3"我的自定义架构中的架构标记。它不包括像xmlns:somequalifier="urn:some-org:v3". 这是造成问题的原因吗?它在没有限定符的 JAXB 中完美运行。遍历整个命名空间并在每种类型前面添加一个限定符将是一个很大的痛苦。

或者,可能是示例项目的 pom.xml 中定义的结构(在上面链接的 zip 文件中)导致了问题。它们不同于基本的 JAXB 工具。

我怎样才能避免这个问题? 最好不要为每种类型添加限定符。我的架构中有几千行。

堆栈跟踪的以下行可能会有所帮助:

[ERROR] Error while parsing schema(s).Location [ file:/C:/path/to/src/main/resources/schema.xsd{4,32}].
org.xml.sax.SAXParseException; systemId: file:/C:/path/to/src/main/resources/schema.xsd; lineNumber: 4; columnNumber: 32; Unsupported binding namespace "". Perhaps you meant "http://annox.dev.java.net"?
    at com.sun.tools.xjc.reader.AbstractExtensionBindingChecker.error(AbstractExtensionBindingChecker.java:213)
    at com.sun.tools.xjc.reader.AbstractExtensionBindingChecker.checkAndEnable(AbstractExtensionBindingChecker.java:115)
    at com.sun.tools.xjc.reader.ExtensionBindingChecker.startElement(ExtensionBindingChecker.java:133)
    at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker.startElement(IncorrectNamespaceURIChecker.java:128)
    at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.reader.xmlschema.parser.CustomizationContextChecker.startElement(CustomizationContextChecker.java:193)
    at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.reader.internalizer.DOMForestScanner$LocationResolver.startElement(DOMForestScanner.java:147)
    at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:244)
    at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:127)
    at com.sun.tools.xjc.reader.internalizer.DOMForestScanner.scan(DOMForestScanner.java:92)
    at com.sun.tools.xjc.reader.internalizer.DOMForestScanner.scan(DOMForestScanner.java:100)
    at com.sun.tools.xjc.reader.internalizer.DOMForestParser.parse(DOMForestParser.java:104)
    at com.sun.tools.xjc.ModelLoader$XMLSchemaParser.parse(ModelLoader.java:269)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:347)
    at com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:128)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:168)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:157)
    at com.sun.tools.xjc.ModelLoader.createXSOM(ModelLoader.java:534)
    at com.sun.tools.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:378)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:174)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:119)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.loadModel(XJC22Mojo.java:50)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:40)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:28)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:318)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:161)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:106)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:317)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
4

1 回答 1

3

您看到的错误是由于缺少 tag 上的前缀<property name="xsid"/>。正确的标签如下

<jaxb:bindings node="//xs:complexType[@name='Section']/xs:attribute[@name='ID']">
    <jaxb:property name="xsid"/>
</jaxb:bindings>

无论如何,您的 XSD 有一些问题,因为 bymaven-jaxb2-plugin可以生成所有类并且maven-hyperjaxb3-plugin是不可能的。


我建议解决您的整个问题。如果您不需要将生成的所有类标记为@Entity 或@Table,则可以使用以下配置。

新京报

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    version="2.1"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:annox="http://annox.dev.java.net"  >

    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:globalBindings generateIsSetMethod="true"/>
        <jaxb:schemaBindings>
            <jaxb:package name="org.jvnet.hyperjaxb3.ejb.tests.pocustomized"/>
        </jaxb:schemaBindings>
        <jaxb:bindings node="//xs:complexType[@name='InfrastructureRoot.typeId']">
            <annox:annotate>
                <annox:annotate annox:class="javax.persistence.Entity">
                </annox:annotate>
                <annox:annotate annox:class="javax.persistence.Table" name="typeId">
                </annox:annotate>
            </annox:annotate>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:complexType[@name='ObservationMedia']/xs:attribute[@name='ID']">
            <jaxb:property name="xsid"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:complexType[@name='RegionOfInterest']/xs:attribute[@name='ID']">
            <jaxb:property name="xsid"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:complexType[@name='Section']/xs:attribute[@name='ID']">
            <jaxb:property name="xsid"/>
        </jaxb:bindings>


    </jaxb:bindings>

</jaxb:bindings>

Maven 插件

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
    </dependencies>
    <configuration>
        <args>
            <arg>-Xannotate</arg>
            <arg>-nv</arg>
        </args>
        <extension>true</extension>
        <schemaDirectory>src/main/resources/</schemaDirectory>
        <schemaIncludes>
            <schemaInclude>schema.xsd</schemaInclude>
        </schemaIncludes>
        <bindingDirectory>src/main/resources/</bindingDirectory>
        <bindingIncludes>
            <include>*.xjb</include>
        </bindingIncludes>
        <debug>true</debug>
        <verbose>true</verbose>
        <episode>true</episode>
        <forceRegenerate>true</forceRegenerate>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.3</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>0.6.3</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

不是最佳实践,但工作正常。

于 2014-10-17T12:42:40.147 回答