Java Reference
In-Depth Information
➤
StockQuote
is a class to wrap access to the service, with methods returning
StockQuoteSoap
objects to access one of the service endpoints.
➤
StockQuoteSoap
is a class to wrap access to an endpoint provided by the service, in this case a
SOAP endpoint. It contains a single method, called
getQuote
, to perform the
GetQuote
operation.
➤
GetQuote
and
GetQuoteResponse
wrap the XML messages themselves.
Based on the classes
wsimport
has created for you from the WSDL file, it becomes very easy to
access SOAP services. You no longer have to be concerned with manually constructing the SOAP
request message with the proper parameters and knowing the endpoints you need to access. Try this
out by creating a new class called
ClientWithWSDL
:
package withwsdl;
import net.webservicex.StockQuote;
import net.webservicex.StockQuoteSoap;
public class ClientWithWSDL {
public static void main(String[] args) {
StockQuote service = new StockQuote();
StockQuoteSoap soapService = service.getStockQuoteSoap();
String stock = soapService.getQuote("IBM");
System.out.println(stock);
}
}
Just as before, this should give you the following output (note that executing this code might take
some time):
<StockQuotes><Stock><Symbol>IBM</Symbol><Last>190.01</Last><Date>4/17/2014</Date>
<Time>4:02pm</Time><Change>-
6.39</Change><Open>187.29</Open><High>190.70</High><Low>187.01</Low>
<Volume>11255493</Volume><MktCap>197.9B</MktCap><PreviousClose>196.40
</PreviousClose><PercentageChange>-3.25%</PercentageChange><AnnRange>
172.19 - 211.98</AnnRange><Earns>14.942</Earns><P-E>13.14</P-E>
<Name>International Bus</Name></Stock></StockQuotes>
You only need to use the service‐providing classes. All the rest is dealt with automati-
cally. The
ObjectFactory
class is used by the service classes to construct the
GetQuote
and
GetQuoteResponse
objects—you do not need to construct them manually, but can instead call your
desired method directly with the stock symbol (“IBM”) as the method argument.
Naturally, since this SOAP service is still returning a single string, you still need to perform the
parsing of this string manually, just like earlier:
package withwsdl;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Search WWH ::
Custom Search