简体   繁体   中英

Unmarshaling of soap response with namespace prefix

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM