UnmarshalException:XML上的意外元素只是Marshaller

拉尔斯·拉德高(Lars Ladegaard)

解组XML时遇到了UnmarshalException(意外元素),而我刚通过marshaller运行过。我看起来像编组过程生成XML,不能将其解组。

ObjectFactory objectFactory = new ObjectFactory();
EjendomSoegType type = objectFactory.createEjendomSoegType();
EjendomSoegningKriterierType krit = new EjendomSoegningKriterierType();
{
    krit.setBy("Skovlunde");
}
type.setEjendomSoegningKriterier(krit);
JAXBElement<EjendomSoegType> soeg = objectFactory.createEjendomSoeg(type);

// create JAXBContext which will be used to update writer
JAXBContext context = JAXBContext.newInstance(EjendomSoegType.class);

// marshall or convert jaxbElement containing element to xml format
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(soeg, writer);

String xml = writer.toString();
System.out.println( xml );

Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader reader = new StringReader(xml);
soeg = (JAXBElement<EjendomSoegType>) unmarshaller.unmarshal(reader);

代码unmarshaller.unmarshal(reader)的最后一行引发以下UnmarshalException

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://rep.oio.dk/tinglysning.dk/service/message/elektroniskakt/1/", local:"EjendomSoeg"). Expected elements are (none)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:609)
     at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:244)

生成的XML看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns8:EjendomSoeg xmlns:ns2="http://rep.oio.dk/tinglysning.dk/schema/model/1/" 
        xmlns:ns4="http://rep.oio.dk/cpr.dk/xml/schemas/core/2005/03/18/" 
        xmlns:ns3="http://rep.oio.dk/kms.dk/xml/schemas/2005/03/11/" 
        xmlns:ns5="http://rep.oio.dk/bbr.dk/xml/schemas/2005/03/11/" 
        xmlns:ns6="http://rep.oio.dk/bbr.dk/xml/schemas/2005/12/15/" 
        xmlns:ns7="http://rep.oio.dk/tinglysning.dk/schema/elektroniskakt/1/" 
        xmlns:ns8="http://rep.oio.dk/tinglysning.dk/service/message/elektroniskakt/1/">
    <ns7:EjendomSoegningKriterier>
        <By>Skovlunde</By>
    </ns7:EjendomSoegningKriterier>
</ns8:EjendomSoeg>

为什么会引发UnmarshalException?

其他信息:ObjectFactory中的createEjendomSoeg方法具有@XmlElementDecl标记。

/**
 * Create an instance of {@link JAXBElement }{@code <}{@link EjendomSoegType }{@code >}}
 * 
 */
@XmlElementDecl(namespace = "http://rep.oio.dk/tinglysning.dk/service/message/elektroniskakt/1/", name = "EjendomSoeg")
public JAXBElement<EjendomSoegType> createEjendomSoeg(EjendomSoegType value) {
    return new JAXBElement<EjendomSoegType>(_EjendomSoeg_QNAME, EjendomSoegType.class, null, value);
}
博多安

上的@XmlElementDecl注释ObjectFactory未使用。要进行ObjectFactory处理,您需要JAXBContext在此类或生成的模型的包上创建。

JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);

如果您要解组的元素不存在@XmlRootElement@XmlElementDecl与之对应,那么您将需要使用unmarshal带有Class参数方法

soeg = unmarshaller.unmarshal(new StreamSource(reader), EjendomSoegType.class);

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“ Group”)

javax.xml.bind.UnmarshalException:意外元素(uri:

Unmarshalexception意外元素,预期元素为(无)

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

如何修复:javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“目录”)。预期元素为(无)

javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“ ClientConfigData”)。期望的元素是<{} clientConfigData>

JaxB Unmarshal异常:javax.xml.bind.UnmarshalException:意外元素。我哪里出问题了?

javax.xml.bind.UnmarshalException:意外元素(uri:“ MyProtocol.xsd”,本地:“ MyFrame”)。预期元素为(无)

javax.xml.bind.UnmarshalException:意外元素(URI:“”,本地:“ NewDataSet”)。期望的元素是<{http://www.webserviceX.NET} string>

Spring Boot Apache Camel(Red Hat Fuse)应用程序为 xml 定义的路由抛出异常:javax.xml.bind.UnmarshalException:意外元素

UnmarshalException:意外元素(uri:“ http://www.namespace.com/RTS”,本地:“容器”)

jaxb抛出javax.xml.bind.UnmarshalException:仅当在osgi捆绑包中运行时,期望的元素为(无)

CXF中的javax.xml.bind.UnmarshalException

Java对象的JAXB xml字符串-意外元素

在锚元素上允许自包含的xss安全吗(只是黑名单javascript:正确)?

CSS边距在块元素上的意外行为

jQuery 只是克隆 VALUE 元素

从未知的JAXBContext(XML)创建JSON的Marshaller

出现意外错误:无法在“元素”上设置“outerHTML”属性:此元素没有父节点

XML解组无法在First元素上运行

子元素上的XML名称空间

为什么画布上的所有 UI 元素都会旋转,即使我只是尝试旋转选定的一个

JAX-WS:javax.xml.bind.UnmarshalException

Jenkins - 解析抛出异常。javax.xml.bind.UnmarshalException

在 Apache Spark 上运行 JPMML 时出现 UnmarshalException

在 xml 或逐行基础上添加 xml 元素的最佳方法

PostgreSQL:意外的数组元素

JAXB 解组:意外元素

意外的节点类型元素