import javax.xml.soap.*; import java.util.Iterator; import java.net.URL; import java.io.*; public class Client { public static void main(String[] args) { try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPMessage message = prepareMessage(); URL endpoint = new URL("https://opendap.co-ops.nos.noaa.gov/axis/services/Predictions"); SOAPMessage response = connection.call(message, endpoint); connection.close(); System.out.print("\nPrinting the message that is being sent: \n\n"); message.writeTo(System.out); System.out.println("\n\n"); /* 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 store 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"); 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; String tagName = null; SOAPElement se = null; if (iterator.hasNext()) { se = (SOAPElement) iterator.next(); iterator = se.getChildElements(); while (iterator.hasNext()) { se = (SOAPElement) iterator.next(); printMetadata(se); tagName = se.getElementName().getLocalName(); if ("data".equals(tagName)) { iterator2 = se.getChildElements(); while (iterator2.hasNext()) { se = (SOAPElement) iterator2.next(); iterator3 = se.getChildElements(); while (iterator3.hasNext()) { se = (SOAPElement) iterator3.next(); printData(se); } } } } } } } catch (SOAPException e) { System.err.println("ERROR: ******* " + e.toString()); } catch (IOException io) { System.err.println("ERROR: ******* " + io.toString()); } } public static SOAPMessage prepareMessage() { SOAPMessage message = null; try { SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(); message = factory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody(); header.detachNode(); message.getMimeHeaders().addHeader("SOAPAction", ""); Name bodyName = soapFactory.createName("getPredictionsAndMetadata", "pred", "https://opendap.co-ops.nos.noaa.gov/axis/webservices/predictions/wsdl"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); //Constructing the body for the request Name name = soapFactory.createName("stationId"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("8454000"); name = soapFactory.createName("beginDate"); symbol = bodyElement.addChildElement(name); symbol.addTextNode("20060601 00:00"); name = soapFactory.createName("endDate"); symbol = bodyElement.addChildElement(name); symbol.addTextNode("20060602 23:59"); name = soapFactory.createName("datum"); symbol = bodyElement.addChildElement(name); symbol.addTextNode("MLLW"); name = soapFactory.createName("unit"); symbol = bodyElement.addChildElement(name); symbol.addTextNode("0"); name = soapFactory.createName("timeZone"); symbol = bodyElement.addChildElement(name); symbol.addTextNode("0"); name = soapFactory.createName("dataInterval"); symbol = bodyElement.addChildElement(name); symbol.addTextNode("60"); } catch (SOAPException e) { System.err.println("ERROR: ******* " + e.toString()); } return message; } public static void printMetadata(SOAPElement se) { String tagName = se.getElementName().getLocalName(); if (tagName != null) { if ("stationId".equals(tagName)) { System.out.println("Printing Metadata \n"); System.out.println("Station ID : " + se.getValue()); } else if ("stationName".equals(tagName)) { System.out.println("station Name : " + se.getValue()); } else if ("latitude".equals(tagName)) { System.out.println("Latitude : " + se.getValue()); } else if ("longitude".equals(tagName)) { System.out.println("Longitude : " + se.getValue()); } else if ("state".equals(tagName)) { System.out.println("State : " + se.getValue()); } else if ("dataSource".equals(tagName)) { System.out.println("Data Source : " + se.getValue()); } else if ("COOPSDisclaimer".equals(tagName)) { System.out.println("COOPS Disclaimer : " + se.getValue()); } else if ("beginDate".equals(tagName)) { System.out.println("Begin Date : " + se.getValue()); } else if ("endDate".equals(tagName)) { System.out.println("End Date : " + se.getValue()); } else if ("datum".equals(tagName)) { System.out.println("Datum : " + se.getValue()); } else if ("unit".equals(tagName)) { System.out.println("Unit : " + se.getValue()); } else if ("timeZone".equals(tagName)) { System.out.println("Time Zone : " + se.getValue()); } else if ("dataInterval".equals(tagName)) { System.out.println("Data Interval : " + se.getValue() + "\n"); System.out.println("Printing the data \n"); } } } public static void printData(SOAPElement se) { String tagName = se.getElementName().getLocalName(); if (tagName != null) { if (tagName.equals("timeStamp")) { System.out.println("Time Stamp : " + se.getValue()); } else if (tagName.equals("pred")) { System.out.println("Pred : " + se.getValue() + "\n"); } } } } /**************************************************** SAMPLE RUN >java Client Printing the message that is being sent: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <HC:getHConstituentsAndMetadata xmlns:HC="https://opendap.co-ops.nos.noaa.gov/axis/webservices/harmonicconstituents/wsdl"> <stationId>8454000</stationId> <unit>0</unit> <timeZone>0</timeZone> </HC:getHConstituentsAndMetadata> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Iterating through the response object to get the values: Printing Metadata Station ID : 8454000 station Name : Providence Latitude : 41.8067 Longitude : -71.4017 State : RI Data Source : USDOC/NOAA/NOS/COOPS(Center for Operational Oceanographic Products and Services) COOPS Disclaimer : The official Tide and Tidal Current prediction tables are published annually on October 1, for the following calendar year. Tide and Tidal Current predictions generated prior to the publishing date of the official tables are subject to change. The enclosed data are based upon the latest information available as of the date of your request. Tide and Tidal Current predictions generated may differ from the official predictions if information for the station requested has been updated since the publishing date of the official tables. Begin Date : 20060601 00:00 End Date : 20060602 23:59 Datum : MLLW Unit : Meters Time Zone : GMT Data Interval : 60 Min Printing the data Time Stamp : 06/01/2006 00:00 Pred : 0.607 Time Stamp : 06/01/2006 01:00 Pred : 0.725 Time Stamp : 06/01/2006 02:00 Pred : 0.916 Time Stamp : 06/01/2006 03:00 Pred : 1.149 Time Stamp : 06/01/2006 04:00 Pred : 1.311 Time Stamp : 06/01/2006 05:00 Pred : 1.301 Time Stamp : 06/01/2006 06:00 Pred : 1.105 Time Stamp : 06/01/2006 07:00 Pred : 0.791 Time Stamp : 06/01/2006 08:00 Pred : 0.456 Time Stamp : 06/01/2006 09:00 Pred : 0.224 Time Stamp : 06/01/2006 10:00 Pred : 0.201 Time Stamp : 06/01/2006 11:00 Pred : 0.314 Time Stamp : 06/01/2006 12:00 Pred : 0.403 Time Stamp : 06/01/2006 13:00 Pred : 0.473 Time Stamp : 06/01/2006 14:00 Pred : 0.641 Time Stamp : 06/01/2006 15:00 Pred : 0.916 Time Stamp : 06/01/2006 16:00 Pred : 1.177 Time Stamp : 06/01/2006 17:00 Pred : 1.283 Time Stamp : 06/01/2006 18:00 Pred : 1.169 Time Stamp : 06/01/2006 19:00 Pred : 0.889 Time Stamp : 06/01/2006 20:00 Pred : 0.555 Time Stamp : 06/01/2006 21:00 Pred : 0.29 Time Stamp : 06/01/2006 22:00 Pred : 0.23 Time Stamp : 06/01/2006 23:00 Pred : 0.372 Time Stamp : 06/02/2006 00:00 Pred : 0.539 Time Stamp : 06/02/2006 01:00 Pred : 0.628 Time Stamp : 06/02/2006 02:00 Pred : 0.726 Time Stamp : 06/02/2006 03:00 Pred : 0.908 Time Stamp : 06/02/2006 04:00 Pred : 1.116 Time Stamp : 06/02/2006 05:00 Pred : 1.235 Time Stamp : 06/02/2006 06:00 Pred : 1.183 Time Stamp : 06/02/2006 07:00 Pred : 0.975 Time Stamp : 06/02/2006 08:00 Pred : 0.689 Time Stamp : 06/02/2006 09:00 Pred : 0.409 Time Stamp : 06/02/2006 10:00 Pred : 0.244 Time Stamp : 06/02/2006 11:00 Pred : 0.266 Time Stamp : 06/02/2006 12:00 Pred : 0.376 Time Stamp : 06/02/2006 13:00 Pred : 0.444 Time Stamp : 06/02/2006 14:00 Pred : 0.518 Time Stamp : 06/02/2006 15:00 Pred : 0.706 Time Stamp : 06/02/2006 16:00 Pred : 0.98 Time Stamp : 06/02/2006 17:00 Pred : 1.206 Time Stamp : 06/02/2006 18:00 Pred : 1.259 Time Stamp : 06/02/2006 19:00 Pred : 1.113 Time Stamp : 06/02/2006 20:00 Pred : 0.845 Time Stamp : 06/02/2006 21:00 Pred : 0.554 Time Stamp : 06/02/2006 22:00 Pred : 0.342 Time Stamp : 06/02/2006 23:00 Pred : 0.316 *************************************************************/