当前位置: 首页 > 知识库问答 >
问题:

UnMarshalException:意外元素(URI:“”,local:“id”)。所需元素为(无)

桑璞
2023-03-14

重要提示:我想说明我已经读过javax.xml.bind.UnmarshalException:意外元素。预期的元素是(无),并尝试了建议的解决方案,但没有成功。

我尝试将XML反序列化为用XJC生成的JAXB类。这是代码

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);
}

这是一个错误:

UnMarshalException:意外元素(URI:“”,local:“id”)。所需元素为(无)

[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]
@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]
}

这是原始的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

共有1个答案

丌官和泰
2023-03-14

我让它起作用。我必须向xml添加 或任何封闭标记,向Java类添加@XMLRootElement(我不知道为什么xjc默认情况下不添加它),并删除由xjc生成的package-info.Java,它包含:

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

我不太理解它在Java代码中的用处。我还尝试添加 ,但得到错误namespace not defined

 类似资料: