使用命名空间前缀解组soap响应 [英] Unmarshaling of soap response with namespace prefix

查看:160
本文介绍了使用命名空间前缀解组soap响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jaxb解组soap响应。

I am trying to unmarshal a soap response using jaxb.

我尝试使用以下代码查找根元素

I tried using the following code to find the root element

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(false);
        DocumentBuilder db;
        try {
            db = dbf.newDocumentBuilder();
             Document d = db.parse(new File("response.xml"));

                Unmarshaller unmarshaller = null;
                Results results = null;
                unmarshaller = JAXBContext.newInstance(Results.class).createUnmarshaller();

                    Node getNumberResponseElt = d.getElementsByTagName("results").item(0);
                    JAXBElement<Results> rs = unmarshaller.unmarshal(new DOMSource(getNumberResponseElt),Results.class);
                    results = rs.getValue();

           }

这是我的样本回复

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<service:ServiceResponse xmlns:out="http://example.com/Person/PersonData/v1" xmlns:service="http://example.com/Person/PersonData/v1/" xmlns:xs4xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<results>
<out:result>
<out:person>
<out:personName>
<out:firstName>First</out:firstName>
<out:lastName>Last</out:lastName>
</out:personName>
<out:gender>M</out:gender>
</out:person></out:result></results></service:ServiceResponse></soapenv:Body></soapenv:Envelope>

我能够获得结果对象,但是当我尝试在结果中访问结果时,由于前缀 out,它显示为null :

Im able to get the results object, but when I try to access the result in results it shows null because of the prefix out:

任何人都可以帮助我解决这个问题吗?

Can anyone help me getting out of this?

更新
任何人都可以帮我吗?

UPDATE: Can anyone help me on this?

我用stax xml解析器解析结果但我看到它中的所有值都为null。

I used stax xml parser to parse to the results but I see all the values in it as null.

推荐答案

我认为问题在于命名空间位置。您可以使用StAX XMLStreamReader来解析XML并前进到正确的元素,然后在此时从XMLStreamReader解组

I think the problem is the namespace location. You can use a StAX XMLStreamReader to parse the XML and advance to the correct element and then unmarshal from the XMLStreamReader at that point

http://blog.bdoughan.com/2012/08/handle-middle-of -xml-document-with-jaxb.html

这篇关于使用命名空间前缀解组soap响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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