JAX-WS + JAXP Tutorial – Building A StockQuote Web Service Client

In this tutorial you will learn how to make a standalone Web Service client using JAX-WS. JAX-WS and JAXP API together, provide an easy way to develop a web service client. This tutorial makes use of an existing online web service instead of creating one from scratch just for making a client. For this tutorial you would need the following:

  • JWSDP 2.0 pack
  • JDK 1.5 or above
  • The required Web Service WSDL
  • Eclipse 3.1 or above

The procedure for creating the client have been broken down into 3 easy steps which are:

  1. Creating the Stubs from WSDL
  2. Building the Web Service Client using JAX-WS
  3. Parsing the Output using JAXP


JAX-WS is an API for creating as well as invoking web services. It succeeds the JAX RPC API and makes it easier by reducing the XML configurations, through the use of annotations available in Java 5. However you don’t need annotations for this tutorial.

JAXP is a wrapper API over the legacy XML parsing API’s such as DOM and SAX and leverages their usage through its generic interfaces. JAXP is typically used in a web service which is implemented with a Document Literal style. This is because such web services return an XML document and not a simple String Value. The XML Document then has to be parsed using the JAXP API.

So let us begin with the tutorial.

1. Creating the Stubs from WSDL

For this tutorial, I am going to use the Stock Quote Service available at webservicex.net. The web service demo shows you an XML document being returned when you enter in a stock ticker in the text box provided. The client which you will create in JAX-WS will invoke the GetQuote service and receive the same document which we will then parse using JAXP.

In order to invoke the GetQuote service you first need to have the stubs. The stubs can be created easily using the import.bat utility provided in the JWSDP pack. The path where you can find this utility is <JWSDP_HOME>/jaxws/bin. For using this utility you must set the JAXWS_HOME environment variable which points to the JAX-WS directory. For example I have set mine as:


Open the command prompt and go the path <JWSDP_HOME>/jaxws/bin. Use the WSDL path of the GetQuote Web Service and type in the following command,

C:\tomcat50-jwsdp\jaxws\bin>wsimport -s . http://www.webservicex.net/stockquote.

This command generates the stubs which are required for invoking the web service, in the current directory. The -s switch is to generate source code of the stubs. We need to use these generated classes in the client.

Now open Eclipse and make a new Java Project. Name it whatever you like. Next, import all the above generated classes into the project. Remember all the classes have to be in the same package as they were generated i.e net.webservicex

2. Building the Web Service Client using JAX-WS

Create a package com.ws.client where we will place our client code. Make a Java Class called StockQuoteClient. You would now have to import a large set of JAR’s which would be required for running the client. The JAR’s which are needed are:

  • jaxws-api.jar, jaxws-rt.jar, jsr181-api.jar present in jaxws/lib
  • jaxb-api.jar, jaxb-impl.jar present in jaxb/lib
  • saaj-api.jar, saaj-impl.jar present in saaj/lib
  • resolver.jar, activation.jar present in jwsdp-shared/lib
  • jsr173_api.jar, sjsxp.jar present in sjsxp/lib
  • jaxp-api.jar present in jaxp/lib

That’s too much indeed! But trust me, you wouldn’t have your web service client running without these libraries. Now write the following code inside the main method of StockQuoteClient

 StockQuote service = new StockQuote();
 StockQuoteSoap port = service.getStockQuoteSoap();
 String quote = port.getQuote("INFY");	

The code is simple. You first create a service instance of StockQuote. Then you get the port on which you want the function to be invoked. Here I have invoked the StockQuoteSoap port.

Once you get the port you are ready to invoke the service which is done by calling port.getQuote(“INFY”) where INFY is the stock ticker symbol for the company INFOSYS.

Now run this program as a Java Application in Eclipse. You would see a long running line of XML returned as a string which contains the details related to INFY stock.

3. Parsing the Output using JAXP

The returned XML output is undesirable since it cannot be read out easily. So now we have to use some API which would easily help us to get the required details in a nice and clean manner. This is where we would use JAXP. Now instead of printing out the output as it is, make a function parseQuote with which we will parse the XML returned. For this, in the main method, change



List<String> items = new ArrayList<String>();
parseQuote(quote, items);

You may be wondering what the List of items is used for. Well, instead of making the method for displaying all the information contained within the XML fragment, we would create the method more nicer, which will only display the contents of the tags actually needed. Hence we pass the tag names in a List as a second argument.

public static void parseQuote(String quote, List<String> items) throws Exception{	

Its been made static so as to access it directly. You can either throw an Exception from the method or implement a try-catch inside the method. Use the below code inside the parseQuote method.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

In the code DocumentBuilderFactory and DocumentBuilder instances are used to get hold the document which we want to parse. The document is nothing but the XML output stored in the quote object. But in order to read the string, we would require to use it as

Document document = builder.parse(new InputSource(new StringReader(quote)))

and not directly. If used directly you would get a java.net.MalformedURLException exception. After getting a hold of the document we use a for in loop to check which items are required to be displayed.

for(String content:items){
	NodeList list = document.getElementsByTagName(content);
	Node item = list.item(0);
	System.out.println(content + " : " + item.getTextContent());	

The function to look out for is getElementsByTagName() which itself finds the tag names and gives the content within them. Remember for using this function, the tag name provided, must be any leaf element or else you would get a javax.xml.stream.XMLStreamException error.

Final Words

This is a very basic way in which you can build a web service client using JAX-WS and JAXP. The tutorial is aimed for getting started with these two API’s together. Further down from here, you can begin exploring in depth the wonderful features that these API’s provide.

You can copy paste the entire code provided here and use directly in your application.

12 thoughts on “JAX-WS + JAXP Tutorial – Building A StockQuote Web Service Client

  1. Pingback: Host News
  2. Hi,
    The articles are really helpful especially this one.
    The presentation is also very comprehensive

  3. Thanks!

    Finally I found this simple enough description on how to build a working Web Service client.

    Other tutorials (Sun, metro, …) are too complicated and mix also the server side development to the story.

  4. This is a nice article. But as a reader/developer, I would be glad if you could also publish another article showing how to develop a web service with JAX-WS in Eclipse

    … Chisty

Leave a Reply

Your email address will not be published. Required fields are marked *