i\'m new to working with SOAP API\'s
I have a soap response from an API
IMO, you should consider using tools to handle SOAP messages instead of doing it on your own.
Examples :
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.
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();
}
}