I have been following this tutorial to use sax parser. If my input is using xml file, then the below line is working fine. But how can I can parse xml which I get as a response from the web service. How to pass soap response as input to sax parser?
new MySaxParser("catalog.xml");
My code
public class soapTest{
private static SOAPMessage createSoapRequest() throws Exception{
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
soapEnvelope.addNamespaceDeclaration("action", "http://www.webserviceX.NET/");
SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement soapElement = soapBody.addChildElement("GetQuote", "action");
SOAPElement element1 = soapElement.addChildElement("symbol", "action");
element1.addTextNode("ticket");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
soapMessage.saveChanges();
System.out.println("----------SOAP Request------------");
soapMessage.writeTo(System.out);
return soapMessage;
}
private static void createSoapResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\n----------SOAP Response-----------");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
public static void main(String args[]){
try{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "http://www.webservicex.net/stockquote.asmx?wsdl";
SOAPMessage soapRequest = createSoapRequest();
//hit soapRequest to the server to get response
SOAPMessage soapResponse = soapConnection.call(soapRequest, url);
// Not able to proceed from here. How to use sax parser here
soapConnection.close();
}catch (Exception e) {
e.printStackTrace();
}
}
How to parse and get the value from xml response.