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("\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");
            SOAPBody responseBody = response.getSOAPBody();
            
            //Checking for errors
            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();  //stations tag
                    iterator2 = se.getChildElements();
                    se = (SOAPElement)iterator2.next();  //station tag
                    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")) {
                            //attributes for a station                            
                            attributeName = soapFactory.createName("ID");
                            System.out.println("Station ID:\t" + se.getAttributeValue(attributeName));  //station ID 
                            attributeName = soapFactory.createName("name");
                            System.out.println("Station Name:\t" + se.getAttributeValue(attributeName));  //station Name  
                        
                             // metadata element
                            iterator3 = se.getChildElements();
                            while (iterator3.hasNext()) {
                                se = (SOAPElement)iterator3.next(); // metadata tag
                                iterator4 = se.getChildElements(); // location and stationType

                                // Get Location
                                se = (SOAPElement)iterator4.next(); // location tag
                                tagName = se.getElementName().getLocalName();
                                if (tagName != null && tagName.equals("location")) {
                                    iterator5 = se.getChildElements(); // lat and long
                            	    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());
                                    }  
                                }    
                                                        	
                                // Get Station Type
                                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());
        }
    }
}    
 
/****************************************************
        SAMPLE RUN

>java Client

Sample Output (The entire output is omitted as it is too long)


******************************************************
Station ID:	8535581
Station Name:	Stone Harbor, Great Channel
Latitude:	39.0567
Longitude:	-74.7650
stationType:	Subordinate


******************************************************
Station ID:	8534139
Station Name:	Tuckerton Creek entrance
Latitude:	39.5089
Longitude:	-74.3247
stationType:	Subordinate


******************************************************
Station ID:	8535055
Station Name:	BIVALVE
Latitude:	39.2325
Longitude:	-75.0328
stationType:	Harmonic


******************************************************/