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

UnmarshalException:XML上的意外元素刚刚被封送程序

锺离马鲁
2023-03-14

在解封XML时得到一个UnmarshalException(意外元素),我刚刚通过Marshaller运行了该元素。我看起来像是封送处理生成了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?

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

共有1个答案

万俟小林
2023-03-14

未提取ObjectFactory上的@XmlElementDecl批注。要处理ObjectFactory,您需要在这个类上或者在您生成的模型的包上创建JAXBContext

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

如果没有@XmlRootElement@XmlElementDecl对应于您尝试反封送的元素,则需要使用接受Class参数的unmarshal方法。

soeg = unmarshaller.unmarshal(new StreamSource(reader), EjendomSoegType.class);
 类似资料:
  • 问题内容: 我正在构建一个脚本,该脚本必须修补XML文件,包括用一个元素列表替换另一个元素列表。以下函数将补丁(涉及到具有相同名称的元素的可能为空的列表)应用于父元素具有相同名称的元素的列表(也可能为空的列表)。(这只是修补逻辑的一小部分)。 为什么在运行代码时出现以下错误? (下面标记了514行。)据我了解,我刚刚验证了该元素存在(因为NodeList是活动的,因此它的第一个条目将始终是下一个匹

  • 我正在使用JAXB解析器将通过http请求发送的XML转换为Java对象,同时根据XSD模式对其进行验证。问题是,当调用unmarshal()方法时,它会引发以下异常: UnMarshalException:意外元素(URI:“http://www.somedomain.com/”,local:“assign”)。所需元素为(无) 如果从根XML元素中删除名称空间,它会引发相同的异常,uri部分为

  • 问题内容: 有谁知道如何修理它。我正在使用Mac OS 10.8.2 问题答案: 在Python 3中是一个函数;它应该是: 正确安装或使用新版本(如果存在错误)。 在Python 3.3上工作正常。

  • 问题内容: 我已经开始执行以下代码 现在,我需要知道我刚刚开始的进程的pid。 问题答案: 由于Java 9类具有新方法long pid(),因此它很简单

  • 我已经在我的Windows-7-64 PC上成功地在visual studio 2015(使用IDE)中开发了WinAPI应用程序。我通常在发布模式下测试该程序。 然后我对我的源代码进行了一些编辑。程序编译和链接没有错误,但程序的行为并不像我预期的那样,所以我切换到调试模式并尝试构建和运行。再次VS编译和链接没有错误,但随后投诉 “无法启动程序'f:\dropbox\blah\x64\Debug\

  • 问题内容: 如何撤消最近执行的mysql查询? 问题答案: 如果将表类型定义为InnoDB,则可以使用事务。您将需要set ,并且可以在发出之后或在查询或会话结束时提交或取消交易。