JAXB unmarshaling忽略SOAP Envelope / Header标记 [英] JAXB unmarshaling Ignoring the SOAP Envelope/Header tags

查看:479
本文介绍了JAXB unmarshaling忽略SOAP Envelope / Header标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a client I am building for accessing a web service. I am using some JAXB generated classes(Netbeans 6.9) to unmarshal my xml data.

尝试从此Web服务解组InputStream响应时,我遇到意外的元素错误,如果我将响应保存到文件,也会出现相同的意外元素错误。

I am getting unexpected element errors when trying to unmarshal the InputStream response from this webservice, I also get the same unexpected element error if I save the response to a file.

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标签(信封,正文,标题),然后运行解组,没有问题。

With the data saved to file I can go in and delete the SOAP tags(envelope,body,headr) and then run the unmarshaling with no problem.

I还没有真正找到一种方法使解组忽略这些标签。有谁知道可以做些什么来忽略这些标签?

I have not really found a way to make the unmarshaling ignore these tags. Does anyone know what can be done to ignore those tags?

这是主流方法和流返回的类。

Here is the main method and the class returned by the stream.

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


    }

生成的类by Netbeans。

The class generated by 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

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

If you receive the XML input as an InputStream you could parse it with StAX and get an XMLStreamReader. You could then advance the XMLStreamReader to the local root element you want to unmarshal and have JAXB unmarshal it.

选项#2

您可以使用 javax.xml.xpath 库选择本地根元素你想用JAXB解组。有关javax.xml.xpath示例,请参阅:

You could use the javax.xml.xpath library to select the local root element that you want to unmarshal with JAXB. For a javax.xml.xpath example see:

  • splitting up of xml file using java

这篇关于JAXB unmarshaling忽略SOAP Envelope / Header标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆