java - 解码异常 : unexpected element on XML just been Marshaller

标签 java xml jaxb

在解码 XML 时,我收到 UnmarshalException(意外元素),我刚刚通过编码器运行了该 XML。我看起来编码过程生成了 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);

代码最后一行抛出以下 UnmarshalException unmarshaller.unmarshal(reader):

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

最佳答案

ObjectFactory 上的 @XmlElementDecl 注释未被选取。要处理 ObjectFactory,您需要在此类或生成的模型的包上创建 JAXBContext

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

如果没有与您尝试解码的元素相对应的 @XmlRootElement@XmlElementDecl,那么您将需要使用采用 Class 参数的 unmarshal 方法。

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

关于java - 解码异常 : unexpected element on XML just been Marshaller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305686/

相关文章:

java - 无法增加 java 堆大小

java - 如何使用Java更新aes加密

java - 一起使用数据提供程序和 TextNG XML

java - Maven - 如何通过命令行为java项目创建pom.xml

java - xmlInputFactory.createXMLStreamReader 在 Weblogic 上给出 null

java - 生成的源中的 Netbeans 注释

java -\n 和\\s+ 在 Java 中不工作

java - 如何将 EditText 的起始边缘放置在屏幕的水平中心?

java - 如何编写具有多个命名空间的正确 XML 以便进行验证?

java - 使用 jaxb2 基础知识生成 toString 方法时,如何使用绑定(bind)文件排除字段