javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:"id")。预期元素是 (none)

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"id"). Expected elements are (none)

重要提示:我想提前说明我已经阅读 javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none) 并尝试了建议的解决方案但没有成功。


我试图将 XML 反序列化为 JAXB class 生成的 xjc。这是代码:

final GeocodPlugin plugin;

final JAXBContext xmlContext = JAXBContext.newInstance(GeocodPlugin.class);
final Unmarshaller xmlDeserializer = xmlContext.createUnmarshaller();

try (final Reader reader = new StringReader(xml)) {
    plugin = (GeocodPlugin) xmlDeserializer.unmarshal(reader);
}

这是错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"id"). Expected elements are (none)

这是由另一个程序 (YAWL) 创建的 XML。我放 [newline] 只是为了表示在 XML 字符串的开头和结尾有一个换行符:

[newline]
  <id>myid</id>
  <jsonrpc>2.0</jsonrpc>
  <method>geocod</method>
  <params>
    <userId>user</userId>
    <imagePathList>a16274073a714a20b434895e94e8c333</imagePathList>
    <imageIdList>a16274073a714a20b434895e94e8c333</imageIdList>
    <outputPathRoot>/home/database/share/CODELET_20190816144726</outputPathRoot>
    <preserveDirectoryMinDepth>-1</preserveDirectoryMinDepth>
    <preserveDirectoryMaxDepth>-1</preserveDirectoryMaxDepth>
    <polarisation>ANY</polarisation>
    [...other params...]
  </params>
[newline]

这是由xjc:

生成的JAXBclass
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "GeocodPlugin", propOrder = {
        "id",
        "jsonrpc",
        "method",
        "params"
    }
)

public class GeocodPlugin {
    @XmlElement(required = true)
    protected String id;
    @XmlElement(required = true)
    protected String jsonrpc;
    @XmlElement(required = true, defaultValue = "geocod")
    protected String method;
    @XmlElement(required = true)
    protected GeocodParameters params;

    [getters and setters]
}

ObjectFactory 没有用,因为它只是一个没有附加注释的工厂。它似乎只是为了向后兼容而继续生成。不管怎样,我也试过了,但我遇到了同样的问题。

我也试过用<root>元素包围XML,并在classGeocodePlugin中添加@XmlRootElement(name = "root"),但这让我觉得很荒谬错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"root"). Expected elements are ({parameters}root)

其中 parameters 是用作 params 元素类型的对象的名称空间,在原始 XSD...

中声明

这是原文XSD:

<?xml version="1.0" ?>
<xs:schema jxb:version="2.0" targetNamespace="parameters" xmlns:enumerations="enumerations" xmlns:parameters="parameters" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:annotation>
    <xs:appinfo>
      <jxb:schemaBindings>
        <jxb:package name="my.plugin.package"/>
      </jxb:schemaBindings>
    </xs:appinfo>
  </xs:annotation>
  <xs:import namespace="enumerations" schemaLocation="total_enumerations_schema.xsd"/>
  <!-- @author Marco Sulla -->
  <xs:complexType name="GeocodParameters">
    <xs:sequence>
      <xs:element name="userId" nillable="true" type="xs:string"/>
      <xs:element maxOccurs="1000" minOccurs="1" name="imagePathList" nillable="true" type="xs:string"/>
      <xs:element maxOccurs="1000" minOccurs="0" name="imageIdList" nillable="false" type="xs:string"/>
      <xs:element default="-1" name="preserveDirectoryMinDepth" nillable="false" type="xs:integer"/>
      <xs:element default="-1" name="preserveDirectoryMaxDepth" nillable="false" type="xs:integer"/>
      <xs:element default="ANY" name="polarisation" nillable="false" type="enumerations:PolarisationEnum"/>
      [other params]
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="GeocodPlugin">
    <xs:sequence>
      <xs:element name="id" type="xs:string"/>
      <xs:element name="jsonrpc" type="xs:string"/>
      <xs:element default="geocod" name="method" type="xs:string"/>
      <xs:element name="params" type="parameters:GeocodParameters"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

我正在使用 Java 8 (Azul ZuluFx 8.0.202) 和 xjc 2.2.8

我让它工作。我不得不在 XML 中添加 <root> 或任何封闭标签,在 Java class 中添加 @XmlRootElement (我不知道为什么 xjc 默认情况下不添加它)并删除由 xjc 生成的 package-info.java,其中包含:

@javax.xml.bind.annotation.XmlSchema(namespace = "parameters")
package my.plugin.package;

我不太了解它在 Java 代码中的用处。我还尝试添加 <parameters:root>,但出现错误 namespace not defined.