import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.soap.*;
public class Client {
public static void main(String[] args) {
try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
Name bodyName = soapFactory.createName("getDataInventory", "", "urn:DataInventory");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
//Constructing the body for the request
Name name = soapFactory.createName("stationId");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("8454000");
System.out.print("\nPrinting the message that is being sent: \n\n");
message.writeTo(System.out);
System.out.println("\n\n");
URL endpoint = new URL("https://opendap.co-ops.nos.noaa.gov/axis/services/DataInventory");
SOAPMessage response = connection.call(message, endpoint);
connection.close();
/*
System.out.println("\nPrinting the respone that was recieved: \n\n");
response.writeTo(System.out);
*/
//Uncoment this part if you want the response to be saved locally in an XML file
/*
FileOutputStream fout = new FileOutputStream("SoapResponse.xml");
response.writeTo(fout);
fout.close();
*/
//You can also stores the response as a String
/*
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
String str = out.toString();
*/
System.out.println("\n\nIterating through the response object to get the values:\n\n");
System.out.println("Printing Metadata \n");
SOAPBody responseBody = response.getSOAPBody();
//Checking for errors
if (responseBody.hasFault()) {
SOAPFault fault = responseBody.getFault();
String actor = fault.getFaultActor();
System.out.println("Fault contains: ");
System.out.println("Fault code: " + fault.getFaultCodeAsName().getQualifiedName());
System.out.println("Fault string: " + fault.getFaultString());
if (actor != null) {
System.out.println("Actor: " + actor);
}
} else {
Iterator iterator = responseBody.getChildElements();
Iterator iterator2 = null;
Iterator iterator3 = null;
Iterator iterator4 = null;
Iterator iterator5 = null;
String tagName = null;
SOAPElement se = null;
Name attributeName = null;
if (iterator.hasNext()) {
se = (SOAPElement) iterator.next();//Station Tag
iterator2 = se.getChildElements();
if (iterator2.hasNext()) {
se = (SOAPElement) iterator2.next();
tagName = se.getElementName().getLocalName();
if (tagName != null && tagName.equals("station")) {
//attributes for a station
attributeName = soapFactory.createName("name");
System.out.println("Station Name : " + se.getAttributeValue(attributeName));//Station Name
attributeName = soapFactory.createName("ID");
System.out.println("Station ID : " + se.getAttributeValue(attributeName));//Station ID
iterator3 = se.getChildElements();
se = (SOAPElement) iterator3.next();//Metadata Tag
iterator4 = se.getChildElements();
while (iterator4.hasNext()) {
se = (SOAPElement) iterator4.next();
tagName = se.getElementName().getLocalName();
if (tagName != null) {
if (tagName.equals("location")) {
iterator5 = se.getChildElements();
while (iterator5.hasNext()) {
se = (SOAPElement) iterator5.next();
tagName = se.getElementName().getLocalName();
if (tagName != null) {
//The name of the tag in this case is 'lat'
if (tagName.equals("lat")) {
System.out.println("Latitude : " + se.getValue());//Latitude
} else if (tagName.equals("long")) {
System.out.println("Longitude : " + se.getValue());//Longitude
} else if (tagName.equals("state")) {
if (se.getValue() == null) {
System.out.println("State : " );//State is Null
} else {
System.out.println("State : " + se.getValue());//State
}
}
}
}
} else if (tagName.equals("date_established")) {
if (se.getValue() == null) {
System.out.println("Established Date : " );//Established Date is Null
} else {
System.out.println("Established Date : " + se.getValue());//Established Date
}
} else if (tagName.equals("shef_id")) {
if (se.getValue() == null) {
System.out.println("SHEF ID : " );//SHEF ID is Null
} else {
System.out.println("SHEF ID : " + se.getValue());//SHEF ID
}
} else if (tagName.equals("deployment_designation")) {
if (se.getValue() == null) {
System.out.println("Deployment Designation : " );//Deployment Designation is Null
} else {
System.out.println("Deployment Designation : " + se.getValue());//Deployment Designation
}
}
}
}
System.out.println("\nPrinting Data Inventory");
System.out.println("\nFirst\t\t\tLast\t\t\tData Type");
while (iterator3.hasNext()) {
se = (SOAPElement) iterator3.next();
attributeName = soapFactory.createName("first");
System.out.print(se.getAttributeValue(attributeName));
attributeName = soapFactory.createName("last");
System.out.print("\t" + se.getAttributeValue(attributeName));
attributeName = soapFactory.createName("name");
System.out.print("\t" + se.getAttributeValue(attributeName) + "\n");
}
}
}
}
}
} catch (SOAPException e) {
System.err.println("ERROR: ******* " + e.toString());
} catch (IOException io) {
System.err.println("ERROR: ******* " + io.toString());
}
}
}
/****************************************************
SAMPLE RUN
>java Client
Iterating through the response object to get the values:
Printing Metadata
Station Name : Providence
station ID : 8454000
Latitude : 41.8071
Longitude : -71.4012
State : RI
Established Date : 1938-06-03
SHEF ID : FOXR1
Deployment Designation : NWLORTS
Printing Data Inventory
First Last Data Type
1999-10-07 12:00 2013-04-03 15:06 Wind
1999-09-22 18:00 2013-04-03 15:06 Air Temperature
1995-09-25 22:00 2013-04-03 15:06 Water Temperature
1999-08-24 19:00 2013-04-03 15:06 Barometric Pressure
1999-08-24 19:00 2001-01-11 07:00 Water Conductivity
2002-04-30 16:00 2012-05-21 16:54 Water Conductivity
2001-01-01 00:00 2013-04-03 15:06 Preliminary 6-Minute Water Level
1996-01-01 00:00 2013-03-31 23:54 Verified 6-Minute Water Level
1979-05-24 11:00 2013-03-31 23:00 Verified Hourly Height Water Level
1979-11-01 04:48 2013-03-31 23:54 Verified High/Low Water Level
1938-06-01 00:00 1947-06-30 23:54 Verified Monthly Mean Water Level
1956-09-01 00:00 1967-04-30 23:54 Verified Monthly Mean Water Level
1967-11-01 00:00 2013-03-31 23:54 Verified Monthly Mean Water Level
******************************************************/