SAAJ Newbie

W

writeameer

Sorry for the dumb question but I need help. The following is a copy
of Request.java from JavaEETutorial.pdf form the java website.
Except, instead of using an imaginary web site that didn't work
(wombat.ztrade.com), I tried to use a real one (webserviceX.net). I
get a java.util.NoSuchElementException.

Help Please !



import javax.xml.soap.*;
import javax.xml.namespace.QName;

import org.w3c.dom.NodeList;

import java.util.Iterator;
import java.net.URL;
public class Request {
public static void main(String[] args) {
try {
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();

SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();

QName bodyName = new QName("http://www.webserviceX.NET/",
"GetQuote","m");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

QName name = new QName("symbol");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("SUNW");

URL endpoint = new URL ("http://www.webservicex.net/
stockquote.asmx");
SOAPMessage response = connection.call(message, endpoint);
connection.close();

SOAPBody soapBody = response.getSOAPBody();

Iterator iterator = soapBody.getChildElements(bodyName);
bodyElement = (SOAPBodyElement)iterator.next();
String lastPrice = bodyElement.getValue();

System.out.print("The last price for SUNW is ");
System.out.println(lastPrice);


}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
 
S

sdemchenko

1) Response.writeTo(System.out) will give a detailed error message.
You need to set mime-type:
message.getMimeHeaders().addHeader("SOAPAction", "http://
www.webserviceX.NET/GetQuote");
2) The service returns XML rather than price:
Iterator iterator = soapBody.getChildElements(new QName("http://
www.webserviceX.NET/", "GetQuoteResponse"));
bodyElement = (SOAPBodyElement)iterator.next();
String lastPrice = bodyElement.getTextContent();
 
W

writeameer

Thank you for the help. I'm getting a SOAP response with "exception"
for GetQuoteResult for the code listed below:
How can I check I'm setting out the headers correctly in the request?


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetQuoteResponse xmlns="http://www.webserviceX.NET/">
<GetQuoteResult>exception</GetQuoteResult>
</GetQuoteResponse>
</soap:Body>
</soap:Envelope>


import javax.xml.soap.*;
import javax.xml.namespace.QName;

import org.w3c.dom.NodeList;

import java.util.Iterator;
import java.net.URL;
public class Request {
public static void main(String[] args) {
try {
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();



SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();

QName bodyName = new QName("http://www.webserviceX.NET/",
"GetQuote","m");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

QName name = new QName("symbol");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("SUNW");

message.getMimeHeaders().addHeader("SOAPAction", "http://
www.webserviceX.NET/GetQuote");
URL endpoint = new URL ("http://www.webservicex.net/
stockquote.asmx");
SOAPMessage response = connection.call(message, endpoint);


connection.close();

SOAPBody soapBody = response.getSOAPBody();
Iterator iterator = soapBody.getChildElements(new QName("http://
www.webserviceX.NET/", "GetQuoteResponse"));
bodyElement = (SOAPBodyElement)iterator.next();
String lastPrice = bodyElement.getTextContent();

System.out.println(lastPrice);

response.writeTo(System.out);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top