Maven Xjc / Jaxb插件无法解析简单的Xsd文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven Xjc / Jaxb插件无法解析简单的Xsd文件相关的知识,希望对你有一定的参考价值。

我正在使用org.jvnet插件并尝试将xsd文件转换为Java Jaxb注释对象,以用于编组和解组。但是,在尝试编译简单的Xsd文件以供使用时出错。

我认为我正在制作一个简单的语法错误,第二次看起来真的很感激。如果有更多细节我应该提供,请告诉我。所有相关文件和消息都包含在下面。

错误信息:

[ERROR] Error while parsing schema(s).Location [ file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.
xsd{6,46}].

org.xml.sax.SAXParseException; systemId:
  file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.xsd;
    lineNumber: 6; columnNumber: 46; src-resolve.4.2: Error resolving component 'xsd:int'.
    It was detected that 'xsd:int' is in namespace 'http://www.w3c.org/2001/XMLSchema',
    but components from this namespace are not referenceable from schema document
    'file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.xsd'.
    If this is the incorrect namespace, perhaps the prefix of 'xsd:int' needs to be changed.
    If this is the correct namespace, then an appropriate 'import' tag should be added to 
    'file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.xsd'.




        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAX
ParseException(ErrorHandlerWrapper.java:203)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Err
orHandlerWrapper.java:134)

File.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3c.org/2001/XMLSchema" targetNamespace="http://asdf.com/asdf">
    <xsd:element name="RandomObject">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="value" type="xsd:int"></xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Pom.hml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>asdf</groupId>
  <artifactId>asdfasdf</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>asdfasdf</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.1</version>
            <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
        </plugin>
    </plugins>
  </build>

</project>
答案

您的xsd命名空间中有错误。

你有:

http://www.w3c.org/2001/XMLSchema

它应该是:

http://www.w3.org/2001/XMLSchema

www.w3c.org对阵www.w3.org

错误日志非常详细:

[ERROR] s4s-elt-schema-ns: The namespace of element 'schema' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.
  line 2 of file:/.../test.xsd

以上是关于Maven Xjc / Jaxb插件无法解析简单的Xsd文件的主要内容,如果未能解决你的问题,请参考以下文章

使用 XJC 进行 XSD 模式解析

XJC:从无效方案生成 JAXB 类

为 JAXB 生成的类添加前缀

如何指定用于 JAXB xjc 生成的类的 XmlAccessorType

JAXB:如何在XSD中指定attr类型时更改XJC生成的类名?

JAXB XJC 代码生成 - Marshaller 生成的 xml 中缺少“schemaLocation”