Java Reference
In-Depth Information
}
private static SOAPMessage createSOAPRequest(String stockSymbol)
throws Exception {
// Construct a new SOAP request message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Construct the SOAP "body" with the method arguments
SOAPBody soapBody = soapMessage.getSOAPBody();
QName bodyName = new QName(SERVICE_HOST, SERVICE_METHOD);
SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName);
SOAPElement soapBodyArgument1 = bodyElement.addChildElement("symbol");
soapBodyArgument1.addTextNode(stockSymbol);
// Add a SOAP action header to the request
// Action: http://www.webserviceX.NET/GetQuote
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", SERVICE_HOST + SERVICE_METHOD);
soapMessage.saveChanges();
return soapMessage;
}
private static void showSOAPResponse(SOAPMessage soapResponse)
throws Exception {
Node responseNode = soapResponse.getSOAPBody().getFirstChild();
String xmlText = responseNode.getTextContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(
new StringReader(xmlText)));
NodeList stockElements = document.getElementsByTagName("Stock");
for (int i = 0; i < stockElements.getLength(); i++) {
System.out.println("----- Stock nr. " + i + " ------");
NodeList infoElements = stockElements.item(i).getChildNodes();
for (int j = 0; j < infoElements.getLength(); j++) {
System.out.println(infoElements.item(j).getNodeName() +
"\t --> " +
infoElements.item(j).getTextContent());
}
}
}
}
If you execute this code, the program will show the following result:
----- Stock nr. 0 ----—
Symbol --> IBM
Last --> 196.40
Search WWH ::




Custom Search