javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“ id”)。预期元素为(无)

马可·苏拉

重要说明:我想提高一下我已经阅读javax.xml.bind.UnmarshalException:意外元素。期望的要素是(无),并且尝试了建议的解决方案而没有成功。


我试图反序列XML化成由JAXB生成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:意外元素(uri:“”,local:“ id”)。预期元素为(无)

这是XML由另一个程序(YAWL创建的我放[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]

这是由JAXB生成xjc

@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没什么用,因为它只是一个没有附加注释的工厂。似乎仅出于向后兼容而继续生成它。无论如何,我也尝试过它,我遇到了同样的问题。

我还尝试XML用一个<root>元素包围并添加@XmlRootElement(name = "root")到类中GeocodePlugin,但这给了我一个荒谬的错误:

javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“ root”)。预期的元素是({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>

我正在使用Java8(Azul ZuluFx 8.0.202)和xjc2.2.8

马可·苏拉

我让它工作。我必须在中添加一个<root>或任何封闭标签XML,添加@XmlRootElement到Java类中(我不知道为什么xjc默认不添加它),并删除package-info.java由生成的xjc包含以下内容的代码:

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章