Skip to main content

Web Client to print weather report for the input City and Country

Java program to print weather report

Steps:

1.Download the WSDL file from the URL http://www.webservicex.net/globalweather.asmx?WSDL
2.Create a java project in Eclipse
3.Copy the WSDL file to java project
4.Expand the java project in Eclipse and right click on the WSDL file and generate webservice client
5.Rename the package name into your source folder to your desired name
6.Add the following Main Class to your package

Main.java
package org.susil.solutions;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
          CharacterData cd = (CharacterData) child;
          return cd.getData();
        }
        return "";
      }
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        String city=args[0];
        String country=args[1];
        if(city.isEmpty()||country.isEmpty()){
            System.out.println("Usage: java -jar Weather.jar Tokyo Japan");
        }else{       
        java.net.URL url=new java.net.URL("http://www.webservicex.net/globalweather.asmx");
        javax.xml.rpc.Service service = null;
        GlobalWeatherSoapStub stub=new GlobalWeatherSoapStub(url,service);
        String weatherXMLRes=stub.getWeather(city,country);
       
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        InputSource inSource=new InputSource();
        inSource.setCharacterStream(new StringReader(weatherXMLRes));
        Document doc = dBuilder.parse(inSource);
        System.out.println("Weather Report for the city:"+city);
        NodeList nodes=doc.getElementsByTagName("CurrentWeather");
        for(int i=0;i<nodes.getLength();i++){
            Element element=(Element)nodes.item(i);
            System.out.println("Location:"+getCharacterDataFromElement((Element)element.getElementsByTagName("Location").item(0)));
            System.out.println("Time:"+getCharacterDataFromElement((Element)element.getElementsByTagName("Time").item(0)));
            System.out.println("Wind:"+getCharacterDataFromElement((Element)element.getElementsByTagName("Wind").item(0)));
            System.out.println("Visibilty:"+getCharacterDataFromElement((Element)element.getElementsByTagName("Visibility").item(0)));
            System.out.println("Sky conditions:"+getCharacterDataFromElement((Element)element.getElementsByTagName("SkyConditions").item(0)));
            System.out.println("Temperature:"+getCharacterDataFromElement((Element)element.getElementsByTagName("Temperature").item(0)));
            System.out.println("Dew Point:"+getCharacterDataFromElement((Element)element.getElementsByTagName("DewPoint").item(0)));
            System.out.println("Humidity:"+getCharacterDataFromElement((Element)element.getElementsByTagName("RelativeHumidity").item(0)));
            System.out.println("Pressure:"+getCharacterDataFromElement((Element)element.getElementsByTagName("Pressure").item(0)));
        }
        }
    }

}

==>Export as Runnable Jar

Test:
c:\JavaProgramingWorkspace>java -jar weather.jar Delhi India
Mar 20, 2016 11:42:21 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Weather Report for the city:Delhi
Location:New Delhi / Palam, India (VIDP) 28-34N 077-07E 233M
Time:Mar 20, 2016 - 01:30 AM EDT / 2016.03.20 0530 UTC
Wind: from the ENE (060 degrees) at 7 MPH (6 KT):0
Visibilty: 1 mile(s):0
Sky conditions: mostly clear
Temperature: 82 F (28 C)
Dew Point: 55 F (13 C)
Humidity: 39%
Pressure: 29.88 in. Hg (1012 hPa)


c:\JavaProgramingWorkspace>java -jar weather.jar Tokyo Japan
Mar 20, 2016 11:47:30 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Weather Report for the city:Tokyo
Location:Tokyo International Airport, Japan (RJTT) 35-33N 139-47E 8M
Time:Mar 20, 2016 - 02:00 AM EDT / 2016.03.20 0600 UTC
Wind: from the SE (140 degrees) at 8 MPH (7 KT):0
Visibilty: greater than 7 mile(s):0
Sky conditions: partly cloudy
Temperature: 59 F (15 C)
Dew Point: 41 F (5 C)
Humidity: 51%
Pressure: 29.83 in. Hg (1010 hPa)



Comments

Post a Comment

Popular posts from this blog

Java Interface

Problem Statement A Java interface can only contain method signatures and fields. Interface can be used to achieve polymorphism. In this problem you will practice your knowledge on interfaces. You are given an interface   AdvancedArithmetic   which contains a method signature   public abstract int divisorSum(int n) . You need to write a class called MyCalculator which implements the interface. divisorSum   function just takes an integer as input and return the sum of all its divisors. For example divisors of 6 are 1,2,3 and 6, so   divisorSum   should return 12. Value of n will be at most 1000. Read the partially completed code in the editor and complete it. You just need to write the MyCalculator class only.   Your class shouldn't be public. Sample Input 6 Sample Output I implemented: AdvancedArithmetic 12 Explanation Divisors of 6 are 1,2,3 and 6. 1+2+3+6=12. import java.util.*; interface AdvancedArithmetic{   public abstract int divisorSum(int n

Problem: Java Exception Handling

Problem Statement Create a class myCalculator which consists of a single method power(int,int). This method takes two integers,   n   and   p , as parameters and finds   n p . If either   n   or   p   is negative, then the method must throw an exception which says "n and p should be non-negative". Please read the partially completed code in the editor and complete it. Your code mustn't be public. No need to worry about constraints, there won't be any overflow if your code is correct. Sample Input 3 5 2 4 -1 -2 -1 3 Sample Output 243 16 java.lang.Exception: n and p should be non-negative java.lang.Exception: n and p should be non-negative import  java.util.*; class myCalculator{     int power(int n,int p) throws java.lang.Exception{         int power=1;         while(p>0){             power=power*(n);             p--;         }                 if(n==0) power=0;                if(n<0 | p<0){       

Google Cloud Shell | Delete instance