重要:我想提前说明我已经阅读过 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: unexpected element (uri:"", local:"id"). Expected elements are (none)
这是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: 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
最佳答案
我让它工作。我必须添加<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
.
关于javax.xml.bind.UnmarshalException : unexpected element (uri :"", 本地 :"id")。预期元素是(无),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57806826/