java - JAXB2 Maven Plugin xjc parse error : org. xml.sax.SAXParseException: Unexpected <xs:element>出现

标签 java soap jaxb wsdl jaxb2-maven-plugin

我想使用 JAXB2 maven 插件从 WSDL 文件生成 Java 对象,以作为客户端使用 soap 服务。

当我将此插件用作“jaxb2:generate”并进行以下配置时:

...
        <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>tr.com.foo.dummy.model</generatePackage>
                    <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
                    <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                    <bindingDirectory>${project.basedir}/src/main/resources</bindingDirectory>
                    <bindingIncludes>
                        <bindingInclude>*.xjb</bindingInclude>
                    </bindingIncludes>
                    <schemaIncludes>
                        <include>*.wsdl</include>
                    </schemaIncludes>
                </configuration>
            </plugin>

...

我收到一条错误消息:

[INFO] --- maven-jaxb2-plugin:0.14.0:generate (default-cli) @ hmbs ---
[INFO] Latest timestamp of the source resources is [2020-03-13 18:10:23.000], earliest timestamp of the target resources is [2020-03-06 18:06:36.000].
[INFO] Sources are not up-to-date, XJC will be executed.
[ERROR] Error while parsing schema(s).Location [ file:/home/yigithan/playground/foo/bar/src/main/resources/wsdl/foo.wsdl{23,87}].
org.xml.sax.SAXParseException: Unexpected <xs:element> appears at line 23 column 87 
...

我的 WSDL 文件是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s1="http://tempuri.org/AbstractTypes"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <wsdl:types>
        <xs:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/" version="1.0">
            <xs:element name="Insert">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" maxOccurs="1" name="custom" type="tns:Custom"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:complexType name="Custom">
                <xs:sequence>
                    <xs:element minOccurs="1" maxOccurs="1" name="foo" type="xs:int"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="bar" type="xs:int"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="baz" type="xs:long"/> 
                    <xs:element minOccurs="1" maxOccurs="1" name="qux" type="xs:dateTime"/>
                </xs:sequence>
            </xs:complexType>
...

而错误位置在包含“qux”元素的行,{23, 87}点是“qux”的type="xs:dateTime"。如您所见,没有任何元素是意外的。还是我遗漏了一些要点?

最佳答案

好的,我已经解决了这个问题。这是一条非常漫长的解决之路。出现了两个问题。第一个,这就是我首先问这个问题的原因,WSDL 文件是作为 DOCX 文件发送给我的,即使我在 WSDL 中复制粘贴了文件的内容,也需要重新格式化文件文件。通过重新格式化,我不是在谈论 xml 结构。这就像一个笑话,但删除空行并重新创建文件有效。

下一个问题作为“意外序列”出现在如下位置:

<s:schema targetNamespace="http://tempuri.org/AbstractTypes">
            <s:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
            <s:complexType name="StringArray">
                <s:complexContent>
                    <s:restriction base="soapenc:Array">
                        <s:sequence>
                            <s:element maxOccurs="unbounded" minOccurs="0" name="String" type="s:string"/>
                        </s:sequence>
                    </s:restriction>
                </s:complexContent>
            </s:complexType>
        </s:schema>

根据我的研究,原因是 JAXB2 无法处理编码类型。因此,如果发生不相关的错误,请首先尝试重构 xml 文件以防 xml 构造错误,然后确保插件与您使用的类型兼容。

maven-jaxrpc-plugin 在这种情况下运行良好。

注意:R2110 在描述中,声明不得扩展或限制 soapenc:Array 类型。

关于java - JAXB2 Maven Plugin xjc parse error : org. xml.sax.SAXParseException: Unexpected <xs:element>出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60672604/

相关文章:

java - JAXB:列表和元素同名

java - 如何获取 XML 文本作为输入流

java - hashmap.get() 的拆箱会产生 NullPointerException

java - 为 StringTokenizer 中的空内容赋值

java - 如何将列内容设为列名 JTable - MySQL

web-services - Soap WSDL ComplexType 被发布为错误的参数类型

Ruby Savon 身份验证

java - JAXB:未定义注释类型 XmlElement 的属性名称

java.lang.IllegalStateException : Target host is null

xml - clojure 命名空间感知 xml 解析器/zipper