I just happened to create a webservice and a client using ASIX for a web service class. As I know that in the environment it is difficult to get things to work and minor issues can halt the process. I am posting a simple Service, Client and my configurations just in case they are helpful to any one. In my environment TOMCAT is the web container and I compile it with AXIS jar files in my classpath, deploy.wsdd is used for deployment:
I use the variable %AXISCLASSPATH% which is set in my environment variables to following:
%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar;
AXIS_LIB is set to: %AXIS_HOME%\lib
AXIS_HOME is set to : C:\axis-1_4
Step 1 is to code the service: In the given scenario the service expects an integer passed to it in the GetDynamicArrayOfRandomNumbers method and it will return an array with the number of elements equal to the number passed in. It will populate the array with random numbers.
import java.util.Random;
/**
* @author Muhammad Asad Siddiqi
* 06/13/2009 Assignment 2 Web Services
* This class is used as the web service and the .class has to be copied under Tomcat/axis/webapps
* deploy.wsdd has to be used for deployment
*/
public class DynamicArrayGeneratorService
{
/*
* This is the remote method that would be called by the client
*/
public int [] GetDynamicArrayOfRandomNumbers (int nArraySize) throws Exception
{
int []arrRandomNumbers = new int[nArraySize];
for (int nCount=0; nCount < nArraySize; nCount++)
{
Random randomGenerator = new Random();
arrRandomNumbers[nCount] = randomGenerator.nextInt((nCount * 4) + 10);
}
return arrRandomNumbers;
}
}
Step 2: Compiling the service can be done by running the following commands from where your code is:
javac -classpath %AXISCLASSPATH% DynamicArrayGeneratorService.java
Step 3 is coding the client, I am copying and pasting the code for the client below:
import java.lang.*;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
/**
*
* @author Asad Siddiqi
* Assignment # 2 Web services
* This class is the client for the service running on localhost
*/
public class DynamicArrayGeneratorClient
{
public static void main(String args[])
{
try
{
int nSizeToReturn = 0;
if ( args == null || args.length != 1 )
{
System.out.println(“The client utility has to pass in the exactly 1 parameter [size of the array]“);
System.exit(1);
}
nSizeToReturn = Integer.parseInt(args[0]);
// Create an instance of the service
Service serArrayGenerator = new Service();
// Create an instance of the Call to call the method
Call call = (Call) serArrayGenerator.createCall();
// Set the Address for connection
call.setTargetEndpointAddress(new java.net.URL(“http://localhost:8080/axis/services/DynamicArrayGeneratorService“));
// Specify the method to call
call.setOperationName(“GetDynamicArrayOfRandomNumbers”);
// Call the remote method
int [] arrReturned = (int[]) call.invoke(new Object [] {new Integer(nSizeToReturn)});
// Print the output
System.out.println(“PRINTING THE NUMBERS RETURNED BY SERVICE :”);
System.out.println(“——————————————”);
for (int i = 0; i < arrReturned.length; i++)
{
System.out.println(arrReturned[i]);
}
}
catch( Exception e )
{
// Error occured during processing
System.out.println(“Error Occured during processing …”);
e.printStackTrace();
}
}
};
Step 4: Compiling the Client can be done by executing the following command from the same directory as your client code:
javac -classpath %AXISCLASSPATH% DynamicArrayGeneratorClient.java
Step 5 : In your command prompt navigate to the directory where your deploy.wsdd file is located. Once you are in the right directory execute the following command:
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd
Step 6: The client can be executed by again changing the directory to where the .class files were generated for the client and executing the following command:
java -cp .;%AXISCLASSPATH% edu.jhu.webservices.Siddiqui.Asad.DynamicArrayGeneratorClient 20
I am also posting the contents of deploy.wsdd file:
<deployment name=”hw2″ xmlns=”http://xml.apache.org/axis/wsdd/“
xmlns:java=”http://xml.apache.org/axis/wsdd/providers/java“>
<service name=”DynamicArrayGeneratorService” provider=”java:RPC”>
<parameter name=”className” value=”DynamicArrayGeneratorService”/>
<parameter name=”allowedMethods” value=”GetDynamicArrayOfRandomNumbers”/>
<parameter name=”wsdlServicePort” value=”GetDynamicArrayOfRandomNumbers”/>
</service>
</deployment>
If you find it useful or I was able to help you out or you have a question, feel free to comment and I will make sure to reply. Being a .Net developer this was a real pain and it sucks, I just posted to help anyone in the same boat as me.
DOM and SAX Parsing using JAVA
July 26, 2009
I am not a very proficient java developer but I can find my way after spending some time. This might help save some time who is just like me and not a real pro at JAVA
. I am posting a sample XML and the parser to parse it in DOM and SAX using Java.
Here is the xml:
<?xml version=”1.0″?>
<!DOCTYPE Syllabus SYSTEM “Syllabus.dtd”>
<Syllabus xmlns:xsi=”http://www.w3.org/2001/XMLSchema” xsi:schemaLocation=”http://www.w3schools.com note.xsd”>
<Lecture SerialNo=”1″>
<Week>1</Week>
<Date>Jun 3</Date>
<Material>Sample Material</Material>
<Reading>Sample Reading </Reading>
<AssignedHW>Sample Text</AssignedHW>
<DueHW></DueHW>
</Lecture>
<Lecture SerialNo=”2″>
<Week>2</Week>
<Date>Jun 10</Date>
<Material>Some Text</Material>
<Reading>Some Boring Text </Reading>
<AssignedHW>HW2</AssignedHW>
<DueHW>HW1</DueHW>
</Lecture>
<Lecture SerialNo=”3″>
<Week>3</Week>
<Date>Jun 17</Date>
<Material>Some Lame material</Material>
<Reading>Lame reading</Reading>
<AssignedHW>HW4</AssignedHW>
<DueHW>HW3</DueHW>
</Lecture>
</Syllabus>
Here is the code:
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Parser
{
public Parser()
{
}
public void doDOMParsing(String strFileName)
{
try
{
java.io.File file = new File(strFileName);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName(“Lecture”);
for (int i = 0; i < nodes.getLength(); i++)
{
System.out.println(“PARSED LECTURE DATA”);
System.out.println(“————-”);
Element element = (Element) nodes.item(i);
NodeList weekData = element.getElementsByTagName(“Week”);
Element line = (Element) weekData.item(0);
String strWeekData = getCharacterDataFromElement(line);
System.out.println(“Week : ” + strWeekData);
NodeList dateData = element.getElementsByTagName(“Date”);
line = (Element)dateData.item(0);
String strDateDate = getCharacterDataFromElement(line);
System.out.println(“Date : ” + strDateDate);
NodeList materialData = element.getElementsByTagName(“Material”);
line = (Element)materialData.item(0);
String strMaterial = getCharacterDataFromElement(line);
System.out.println(“Material : ” + strMaterial);
NodeList readingData = element.getElementsByTagName(“Reading”);
line = (Element)readingData.item(0);
String strReading = getCharacterDataFromElement(line);
System.out.println(“Reading : ” + strReading);
NodeList assignedHWData = element.getElementsByTagName(“AssignedHW”);
line = (Element)assignedHWData.item(0);
String strAssignedHW = getCharacterDataFromElement(line);
System.out.println(“Assigned HomeWork : ” + strAssignedHW);
NodeList dueHWData = element.getElementsByTagName(“DueHW”);
line = (Element)dueHWData.item(0);
String strDueHW = getCharacterDataFromElement(line);
System.out.println(“Due HomeWork : ” + strDueHW);
System.out.println(“——————————–”);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/* This is the helper function to get Element Data in DOM Parsing */
private String getCharacterDataFromElement(Element e)
{
try
{
Node child = e.getFirstChild();
if(child instanceof CharacterData)
{
CharacterData cd = (CharacterData) child;
return cd.getData();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return “”;
}
/* This is the innerclass that inherits from Default Handler for SAX Parsing */
class SAXHandler extends DefaultHandler
{
boolean bWeek = false;
boolean bDate = false;
boolean bMaterial = false;
boolean bReading = false;
boolean bAssignedHW = false;
boolean bDueHW = false;
public void startElement(String nsURI, String strippedName,String tagName, Attributes attributes) throws SAXException
{
if (tagName.equalsIgnoreCase(“Week”))
bWeek = true;
if (tagName.equalsIgnoreCase(“Date”))
bDate = true;
if (tagName.equalsIgnoreCase(“Material”))
bMaterial = true;
if (tagName.equalsIgnoreCase(“Reading”))
bReading = true;
if (tagName.equalsIgnoreCase(“AssignedHW”))
bAssignedHW = true;
if (tagName.equalsIgnoreCase(“DueHW”))
bDueHW = true;
}
public void characters(char[] ch, int start, int length)
{
if (bWeek)
{
System.out.println(“Week : ” + new String(ch, start, length));
bWeek = false;
}
else if (bDate)
{
System.out.println(“Date : ” + new String(ch, start,length));
bDate = false;
}
else if (bMaterial)
{
System.out.println(“Material : ” + new String(ch, start,length));
bMaterial = false;
}
else if (bReading)
{
System.out.println(“Reading : ” + new String(ch, start,length));
bReading = false;
}
else if (bAssignedHW)
{
System.out.println(“Assigned Homework : ” + new String(ch, start,length));
bAssignedHW = false;
}
else if (bDueHW)
{
System.out.println(“Due Homework : ” + new String(ch, start,length));
bDueHW = false;
}
}
}
/* This is the entry point where SAX Parsing starts */
public void doSAXParsing(String strFileName )
{
try
{
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
SAXHandler handler = new SAXHandler();
saxParser.parse(new File(strFileName),handler);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
/* This is the driver function from where the DOM AND SAX parsing has to be called
The file name has to be passed in like “Syllabus.xml”
*/
public static void main(String []args)
{
try
{
String strFileName = “..\\resources\\Syllabus.xml”;
String strParsingMode=”";
if (args.length != 1)
{
System.out.println(“The valid values of the parameter passed in are DOM and SAX. Press any key to continue:”);
int a = System.in.read();
System.exit(0);
}
Parser driverParser = new Parser();
strParsingMode = args[0];
if (strParsingMode.equalsIgnoreCase(“dom”))
{
System.out.println(“DOM PARSING”);
driverParser.doDOMParsing(strFileName);
}
else if (strParsingMode.equalsIgnoreCase(“sax”))
{
System.out.println(“SAX PARSING”);
driverParser.doSAXParsing(strFileName);
}
else
{
System.out.println(“The parsing mode can be DOM or SAX”);
System.exit(0);
}
}
catch(Exception ex)
{
System.out.println(“Error Occured while processing”);
ex.printStackTrace();
}
}
}
To me how SAX parsing is implemented using Inner classes was really interesting . I hope it helps someone looking for some help and finds it