Unmarshaling of soap response with namespace prefix

拥有回忆 提交于 2019-12-24 00:53:52

问题


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

           }

Here is my sample response

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

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?

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


回答1:


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



来源:https://stackoverflow.com/questions/17483699/unmarshaling-of-soap-response-with-namespace-prefix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!