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();

            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/RelativeHumidity");
            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 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 = (SOAPElementiterator.next();
                    iterator = se.getChildElements();
                    while (iterator.hasNext()) {
                        se = (SOAPElementiterator.next();
                        printMetadata(se);
                        tagName = se.getElementName().getLocalName();
                        if ("data".equals(tagName)) {
                            iterator2 = se.getChildElements();
                            while (iterator2.hasNext()) {
                                se = (SOAPElementiterator2.next();
                                iterator3 = se.getChildElements();
                                while (iterator3.hasNext()) {
                                    se = (SOAPElementiterator3.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();

            SOAPBody body = message.getSOAPBody();
            Name bodyName = soapFactory.createName("getRelativeHumidityAndMetadata""relativehumidity""https://opendap.co-ops.nos.noaa.gov/axis/webservices/relativehumidity/wsdl");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

            //Constructing the body for the request
            Name name = soapFactory.createName("stationId");
            SOAPElement symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("9753216");
            name = soapFactory.createName("beginDate");
            symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("20090601 09:00");
            name = soapFactory.createName("endDate");
            symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("20090601 09:30");
            name = soapFactory.createName("timeZone");
            symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("0");
        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 ("unit".equals(tagName)) {
                System.out.println("Unit             : " + se.getValue());
            else if ("timeZone".equals(tagName)) {
                System.out.println("Time Zone        : " + 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("RH")) {
                System.out.println("RH          : " + se.getValue());
            else if (tagName.equals("X")) {
                System.out.println("X           : " + se.getValue());
            }else if (tagName.equals("N")) {
                System.out.println("N           : " + se.getValue());
            }  else if (tagName.equals("R")) {
                System.out.println("R           : " + 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:Header/>
<SOAP-ENV:Body>
<relativehumidity:getRelativeHumidityAndMetadata xmlns:relativehumidity="https://opendap.co-ops.nos.noaa.gov/axis/webservices/relativehumidity/wsdl">
<stationId>9753216</stationId>
<beginDate>20090601 09:00</beginDate>
<endDate>20090601 09:30</endDate>
<timeZone>0</timeZone>
</relativehumidity:getRelativeHumidityAndMetadata>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Iterating through the response object to get the values:


Printing Metadata

Station ID       : 9753216
station Name     : Fajardo
Latitude         : 18.3352
Longitude        : -65.6311
State            : PR
Data Source      : USDOC/NOAA/NOS/COOPS(Center for Operational Oceanographic Products and Services)
COOPS Disclaimer : These raw data have not been subjected to the National Ocean Service's quality control
or quality assurance procedures and do not meet the criteria and standards of
official National Ocean Service data. They are released for limited public use as
preliminary data to be used only with appropriate caution.
Begin Date       : 20090601 09:00
End Date         : 20090601 09:30
Time Zone        : GMT

Printing the data

Unit             : Relative Humidity is in Percentage
Time Stamp  : 2009-06-01 09:00:00.0
RH          : 76
X           : 0
N           : 0
R           : 0

Time Stamp  : 2009-06-01 09:06:00.0
RH          : 77
X           : 0
N           : 0
R           : 0

Time Stamp  : 2009-06-01 09:12:00.0
RH          : 80
X           : 0
N           : 0
R           : 0

Time Stamp  : 2009-06-01 09:18:00.0
RH          : 81
X           : 0
N           : 0
R           : 0

Time Stamp  : 2009-06-01 09:24:00.0
RH          : 82
X           : 0
N           : 0
R           : 0

Time Stamp  : 2009-06-01 09:30:00.0
RH          : 84
X           : 0
N           : 0
R           : 0
************************************************************/