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

JAXB解组,忽略SOAP信封/头标记

祁烨
2023-03-14

我有一个正在构建的用于访问Web服务的客户端。我正在使用一些JAXB生成的类(Netbean 6.9)来散集我的xml数据。

当试图从这个Web服务解组InputStream响应时,我会遇到意外的元素错误,如果我将响应保存到文件中,我也会遇到同样的意外元素错误。

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2003/05/soap-envelope", local:"Envelope"). Expected elements are <{http://www.cmicdataservices.com/}Authentication>,....

使用保存到文件中的数据,我可以进入并删除SOAP标签(信封、正文、标头),然后毫无问题地运行解组。

我还没有真正找到一种方法让未婚夫忽略这些标签。有谁知道可以做些什么来忽略这些标签?

下面是主方法和流返回的类。

   public static void main(String[] args) {
        JAXBContext jaxbContext = null;
        try {
            CMICData cmic = new CMICData();
            jaxbContext = JAXBContext.newInstance("cmic.ajrs.com");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();


            GetCurrentDataVer1Response response = (GetCurrentDataVer1Response)
                    unmarshaller.unmarshal( cmic.getCMICIs("GetCurrentDataVer1"));
            DatacenterDataVer1 dataSet = response.getGetCurrentDataVer1Result();

            List products = dataSet.getAProductBase().getProductBase();
            // print some primary keys to show data being processed.
            for(Iterator<ProductBase> iter = products.iterator(); iter.hasNext();) {
                ProductBase pb = iter.next();
                System.out.println(pb.getPkID());
            }

        } catch (JAXBException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

由 Netbeans 生成的类。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "getCurrentDataVer1Result"
})
@XmlRootElement(name = "GetCurrentDataVer1Response", namespace = "http://www.cmicdataservices.com/")
public class GetCurrentDataVer1Response {

    @XmlElement(name = "GetCurrentDataVer1Result")
    protected DatacenterDataVer1 getCurrentDataVer1Result;

    /**
     * Gets the value of the getCurrentDataVer1Result property.
     * 
     * @return
     *     possible object is
     *     {@link DatacenterDataVer1 }
     *     
     */
    public DatacenterDataVer1 getGetCurrentDataVer1Result() {
        return getCurrentDataVer1Result;
    }

    /**
     * Sets the value of the getCurrentDataVer1Result property.
     * 
     * @param value
     *     allowed object is
     *     {@link DatacenterDataVer1 }
     *     
     */
    public void setGetCurrentDataVer1Result(DatacenterDataVer1 value) {
        this.getCurrentDataVer1Result = value;
    }

}

共有1个答案

段溪叠
2023-03-14

有几种不同的选择:

选项#1

如果您以 InputStream 的形式接收 XML 输入,则可以使用 StAX 对其进行解析并获取 XMLStreamReader。然后,您可以将 XMLStreamReader 推进到要取消元帅的本地根元素,并让 JAXB 取消元帅。

选项2

您可以使用javax.xml.xpath库来选择要与JAXB散集的本地根元素。有关javax.xml.xpath示例,请参见:

  • 使用 java 拆分 xml 文件
 类似资料:
  • 问题内容: 我有一个正在建立的用于访问Web服务的客户端。我正在使用一些JAXB生成的类(Netbeans 6.9)来解组我的xml数据。 尝试从此Web服务解组InputStream响应时遇到意外元素错误,如果将响应保存到文件中,也会遇到相同的意外元素错误。 将数据保存到文件后,我可以进入并删除SOAP标签(信封,正文,标题),然后毫无问题地进行解组。 我还没有真正找到一种使拆封忽略这些标签的方

  • 对象使用JAXBContext创建的封送器进行封送。生成的xml将变成: 类Hi是从不能更改的xsd生成的。我的问题是,如果“hello”为null,有没有一种方法可以让封送器忽略nillable参数,并且不向xml输出任何内容?

  • 当我试图使用包含名称空间的JAXB将xml转换为Java对象时,会发生错误。 示例示例:' Zeta Walnes ` 我需要忽略名称空间

  • 我花了一些时间来调查是什么问题,但我不能解决它。当我在XML下面反封送和回封送时,我看到的是不同的XML。 在上面的xml中,只有一个标记(第一个),其余所有(包括名称空间)都是意外元素。另一个应用程序发送上述XML。 null 我真的不明白QName的事

  • 只有在soap信封已被移除的情况下,JAXB才能解组XML。然而,我试图解组的SOAP响应在SOAP信封上有其名称空间声明。如果删除soap信封,名称空间声明也将被删除。因此,标签的前缀将指无。这会导致JAXB抛出错误。如果在SOAP信封上声明了名称空间,如何使用JAXB对SOAP响应进行解组? 我需要解组的XML的类似示例如下: 如果我拿掉肥皂信封,会发生这样的事情: 如果我移除soap信封,名

  • 问题内容: 如果已删除肥皂信封,JAXB只能解组XML。但是,我要解组的SOAP响应在soap信封上有其名称空间声明。如果我删除肥皂信封,则名称空间声明也将被删除。因此,标签的前缀将不涉及任何前缀。这将导致JAXB引发错误。如果在SOAP信封上声明了名称空间,如何使用JAXB解组SOAP响应? 我需要解组的XML的类似示例如下: 如果我取下肥皂信封,会发生以下情况: 如果删除了肥皂信封,则名称空间