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{   p...

change directory (cd) function for an abstract file system ( Java Implementation )

Write a function that provides change directory (cd) function for an abstract file system. Notes: Root path is '/'. Path separator is '/'. Parent directory is addressable as "..". Directory names consist only of English alphabet letters (A-Z and a-z). For example, new Path("/a/b/c/d").cd("../x").getPath() should return "/a/b/c/x". Note: The evaluation environment uses '\' as the path separator. public class Path {     private String path;     public Path(String path) {         this.path = path;     }     public String getPath() {         return path;     }     public Path cd(String newPath) {         //throw new UnsupportedOperationException("Waiting to be implemented."); String[] newP=newPath.split("/");     String[] oldP=path.split("/");     int lnCount=0;     for(String str:newP){     if(st...

Java Abstract Class

Problem Statement A Java abstract class is a class that can't be instantiated, that means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritence before attempting this challenge. Following is an example of abstact class: abstract class Book {     String title;     abstract void setTitle(String s);     String getTitle()     {         return title;     } } If you try to create an instance of this class like the following line you will get an error: Book new_novel=new Book(); You have to create another class that extends the abstract class. Then you can create instance of the new class. Notice that   setTitle   method is abstract too and has no body. That means you must implement the body of the that method in the child class. In the editor we have provided ...