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("getHistoricStations", "", "urn:HistoricStations");
            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/HistoricStations");
            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("name");
                            System.out.print("Station Name     : " + se.getAttributeValue(attributeName));//Station Name  
                            attributeName = soapFactory.createName("ID");
                            System.out.print("\nStation ID       : " + se.getAttributeValue(attributeName));//Station ID 

                            iterator3 = se.getChildElements();
                            se = (SOAPElement) iterator3.next();//Metadata Tag
                            iterator4 = se.getChildElements();
                            se = (SOAPElement) iterator4.next();//Location Tag 
                            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.print("\nLatitude         : " + se.getValue());//Latitude
                                    } else if (tagName.equals("long")) {
                                        System.out.print("\nLongitude        : " + se.getValue());//Longitude
                                    } else if (tagName.equals("state")) {
                                        if (se.getValue() == null) {
                                            System.out.print("\nState            : " );//State is Null
                                        } else {
                                            System.out.print("\nState            : " + se.getValue());//State
                                        }
                                    }                                   
                                }                            
                            }

                            se = (SOAPElement ) iterator4.next();//Established Date Tag
                            tagName = se.getElementName().getLocalName();
                            if (tagName != null && tagName.equals("date_established")) {
                                if (se.getValue() == null) {
                                    System.out.print("\nEstablished Date : ");//Established Date is Null 
                                } else {
                                    System.out.print("\nEstablished Date : " + se.getValue());//Established Date 
                                }
                            }

                            se = (SOAPElement ) iterator4.next();//Removed Date Tag
                            tagName = se.getElementName().getLocalName();
                            if (tagName != null && tagName.equals("date_removeded")) {
                                if (se.getValue() == null) {
                                    System.out.println("\nRemoved Date : ");//Removed Date is Null 
                                } else {
                                    System.out.println("\nRemoved Date : " + se.getValue());//Removed Date 
                                }
                            }
                        }               
                    }                 
                }        
            }  
        } 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 for it is too long)

******************************************************
Station Name     : Barbuda
Station ID       : 9761115
Latitude         : 17.5908
Longitude        : -61.8206
State            : Antigua and Barbuda
Established Date : 2011-05-24
Removed Date : present


******************************************************
Station Name     : NAOS ISLAND, PANAMA BAY
Station ID       : 9812530
Latitude         : 8.9183
Longitude        : -79.5333
State            : Panama
Established Date : 1991-08-25
Removed Date : 1998-01-28


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