java JAXB 解组忽略 SOAP Envelope/Header 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5568852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:42:30  来源:igfitidea点击:

JAXB unmarshaling Ignoring the SOAP Envelope/Header tags

javaweb-servicesjaxb

提问by grantk

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 服务。我正在使用一些 JAXB 生成的类(Netbeans 6.9)来解组我的 xml 数据。

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.

尝试解组来自该 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>,....

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.

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

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


    }

The class generated by Netbeans.

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

}

采纳答案by bdoughan

There are a couple of different options:

有几个不同的选项:

Option #1

选项1

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

如果您收到 XML 输入作为InputStream您可以使用 StAX 解析它并获得XMLStreamReader. 然后,您可以将 推进XMLStreamReader到要解组的本地根元素,并让 JAXB 对其进行解组。

Option #2

选项#2

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

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