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

时间:2019-09-05 13:40:19

标签: java xml jaxb deserialization

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


我试图将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>

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

1 个答案:

答案 0 :(得分:0)

我让它工作。我必须在<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