【问题标题】:Unmarshal hits unexpected element in nested namespacesUnmarshal 在嵌套命名空间中命中意外元素
【发布时间】:2025-12-23 20:35:11
【问题描述】:

为了找出我的 SOAP 服务得到的原因,我进行了多次搜索:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://xsd.banverket.se/T/Opera/Tagrapportering2.xsd", local:"Tagrapportering"). Expected elements are <{urn:BVTAF}Tagrapportering>,<{urn:BVTAF}MessageHeader>

当我的网络服务收到这个 xml 时:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MessageArray xmlns="urn:BVTAFMessageInbox">
<BVTagrapportering xmlns="urn:BVTAF">
    <MessageHeader><MessageStatus>1</MessageStatus><MessageReference><MessageType MessageTypeCode="0"/><MessageNumber>5572098</MessageNumber><MessageDateTime>2017-10-31T19:56:22.4312743+01:00</MessageDateTime></MessageReference><Sender>10001</Sender><Recipient>10040</Recipient></MessageHeader>
    <Tagrapportering xmlns="http://xsd.banverket.se/T/Opera/Tagrapportering2.xsd">
        <Tag tagnr="xxxxx" avgangsdatum="2017-10-31" tagslag="RST" dest="M" trafikutovare="Trans...(I intentionally clipped the xml here)

Unmarshaller 固执地说它希望 Tagrapportering 在 urn:BVTAF 命名空间中,但它不在该命名空间中,这是我的 BVTagrapportering 类的开始:

/**
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
  *       &lt;sequence>
  *         &lt;element name="MessageHeader" type="{urn:BVTAF}MessageHeader"/>
  *         &lt;element name="Tagrapportering" type="{http://xsd.banverket.se/T/Opera/Tagrapportering2.xsd}Tagrapportering"/>
  *       &lt;/sequence>
  *     &lt;/restriction>
  *   &lt;/complexContent>
  * &lt;/complexType>
  * </pre>
  * 
  * 
  */

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "",
    propOrder = {
     "messageHeader",
     "tagrapportering",
    }
 )
 @XmlRootElement(name = "BVTagrapportering" )
 public class BVTagrapportering {

     @XmlElement(name = "MessageHeader", namespace = "urn:BVTAF", required = true)
     protected MessageHeader messageHeader;
     @XmlElement(name = "Tagrapportering", namespace = "http://xsd.banverket.se/T/Opera/Tagrapportering2.xsd", required = true)
     protected Tagrapportering tagrapportering;

在我的包中,我有这个 package.info.java:

@javax.xml.bind.annotation.XmlSchema(namespace = "urn:BVTAF", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package bvtaf;

问题是 IF 我从传入的 SOAP 消息中删除了 Tagrapportering 元素中的 xmlns 属性,解组就像一个魅力,但我不控制传入的 SOAP 消息,所以这种方法对我来说不是可行的方法......

我正在使用从 wsdl 到 java 的 Apache CXF 3.2.1 Web 服务代码生成器来生成我的类,并在 Tomcat 8.5 中运行它。

谁能帮帮我!?这让我发疯了......

【问题讨论】:

    标签: xml soap xml-parsing cxf unmarshalling


    【解决方案1】:

    我的错,这个问题的痛苦令人尴尬的答案是我的编译器默默地停止编译和发布我的更改(就在我将命名空间定义添加到上述 java 类中的 @XmlElements 之前,这显然解决了我的问题,尽管我从来没有意识到这一点,因为我的编译器已经停止工作)...

    我的编译器停止的原因是我前段时间升级了我的 Mac OS,这显然破坏了 Eclipse 环境 (After OSX upgrade, eclipse is missing applescriptengine.jar)。我没有注意到我的环境崩溃了,因为 Eclipse 对此非常微妙,我只是继续做我正在做的事情。

    我向所有可能在我的问题上花费时间的人道歉 - 尽管由于明显的原因没有人回答。

    【讨论】: