Convert Soap XML response to Object

后端 未结 2 1070
野的像风
野的像风 2020-12-11 08:04

i\'m new to working with SOAP API\'s

I have a soap response from an API



        
相关标签:
2条回答
  • 2020-12-11 08:19

    IMO, you should consider using tools to handle SOAP messages instead of doing it on your own.

    Examples :

    • Java EE 5 Tutorial : Creating a Simple Web Service and Client with JAX-WS
    • Spring Boot : Consuming a SOAP message

    EDIT

    There's a few things to say about your comment so I'll put my answer here.

    First,

    I have nothing to do with the API, all I do is make a POST request...

    You have nothing to do with the API but you make a POST request to the API. I think this is a figure of speech, right ?...

    and there's no wsdl....

    You can almost always get the WSDL of a SOAP webservice with this little trick. Just add ?wsdl at the end of the SOAP webservice URL.

    Example :

    Here's the URL of a SOAP webservice on the web (a real one) : http://www.webservicex.com/stockquote.asmx

    You can get its WSDL like this : http://www.webservicex.com/stockquote.asmx?wsdl

    So the only option is to parse the response

    IMO, there's almost always more than one solution to a problem in software development.

    0 讨论(0)
  • 2020-12-11 08:36

    you can use this code to retrieve a POJO, and also add an @XmlRootElement as header to your POJO.

    (I did'nt test the code below)

    XMLInputFactory xif = XMLInputFactory.newFactory();
            XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
            xsr.nextTag(); // Advance to Envelope tag
    
            xsr.nextTag(); // Advance to Body tag
            xsr.nextTag();
            xsr.nextTag();
    
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            StringWriter stringWriter = new StringWriter();
            transformer.transform(new StAXSource(xsr), new StreamResult(stringWriter));
            StringReader sr = new StringReader(stringWriter.toString());
            JAXBContext jaxbContext = JAXBContext.newInstance(LoginResult.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            LoginResult loginResult = (LoginResult) unmarshaller.unmarshal(sr);
    

    EDIT :

    I found a solution for you:

        @XmlRootElement(name = "LoginResult", namespace = "http://test.org/ADMail_Service")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class LoginResult {
        @XmlElement(name = "ErrorMessage", namespace = "http://test.org/ADMail_Service")
        private String errorMessage;
        @XmlElement(name = "Status", namespace = "http://test.org/ADMail_Service")
        private String status;
    
        public String getErrorMessage() {
            return errorMessage;
        }
    
        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    
    
    public static void main(String[] args) {
            try {
                XMLInputFactory xif = XMLInputFactory.newFactory();
                XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
                xsr.nextTag(); // Advance to Envelope tag
    
                xsr.nextTag(); // Advance to Body tag
                xsr.nextTag();
                xsr.nextTag();
    
    
                JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
                Unmarshaller unmarshaller = jc.createUnmarshaller();
                JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);
    
                System.out.println(je.getName());
                System.out.println(je.getValue());
                System.out.println(je.getValue().getErrorMessage());
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } catch (JAXBException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题