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();
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
Name bodyName = soapFactory.createName("getTidePredictionStations", "", "urn:TidePredictionStations");
body.addBodyElement(bodyName);
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/TidePredictionStations");
SOAPMessage response = connection.call(message, endpoint);
connection.close();
System.out.println("\n\nIterating through the response object to get the values:\n\n");
SOAPBody responseBody = response.getSOAPBody();
if (responseBody.hasFault()) {
SOAPFault newFault = responseBody.getFault();
System.out.println("SAOP FAULT:\n");
System.out.println("code = " + newFault.getFaultCodeAsName());
System.out.println("message = " + newFault.getFaultString());
System.out.println("actor = " + newFault.getFaultActor());
} else {
Iterator iterator = responseBody.getChildElements();
Iterator iterator2, iterator3, iterator4, iterator5;
String tagName = null;
SOAPElement se = null;
Name attributeName = null;
if (iterator.hasNext()) {
se = (SOAPElement)iterator.next();
iterator2 = se.getChildElements();
se = (SOAPElement)iterator2.next();
iterator2 = se.getChildElements();
while (iterator2.hasNext()) {
se = (SOAPElement)iterator2.next();
tagName = se.getElementName().getLocalName();
System.out.println("\n\n******************************************************");
if (tagName != null && tagName.equals("station")) {
attributeName = soapFactory.createName("ID");
System.out.println("Station ID:\t" + se.getAttributeValue(attributeName));
attributeName = soapFactory.createName("name");
System.out.println("Station Name:\t" + se.getAttributeValue(attributeName));
iterator3 = se.getChildElements();
while (iterator3.hasNext()) {
se = (SOAPElement)iterator3.next();
iterator4 = se.getChildElements();
se = (SOAPElement)iterator4.next();
tagName = se.getElementName().getLocalName();
if (tagName != null && tagName.equals("location")) {
iterator5 = se.getChildElements();
while (iterator5.hasNext()){
se = (SOAPElement)iterator5.next();
tagName = se.getElementName().getLocalName();
if (tagName !=null && tagName.equals("lat")) {
System.out.print("Latitude:");
} else if (tagName!= null && tagName.equals("long")) {
System.out.print("Longitude:");
}
System.out.println("\t" + se.getValue());
}
}
se = (SOAPElement)iterator4.next();
tagName = se.getElementName().getLocalName();
if (tagName != null && tagName.equals("stationType")) {
System.out.println("StationType:\t" + se.getValue());
}
}
}
}
}
}
} catch (SOAPException e) {
System.err.println("ERROR: ******* " + e.toString());
} catch (IOException io) {
System.err.println("ERROR: ******* " + io.toString());
}
}
}