Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;
Import Java.net.URL;
Import Java.text.DateFormat;
Import Java.util.Date;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import org.w3c.dom.Document;
Import org.w3c.dom.NodeList; /** * @description Get Google's simple weather forecast via Java * @author Tianyu * @link http://weibo.com/10792402 * * public class Getgooglew
Eather {/** * * * @param cityname * Note that the pinyin written to the city is converted to a line, which is then extracted in XML format after it is opened. * @return */public string GetWeather (string cityname, String fileaddr) {//Get weather on Google, write file try {URL url =
New URL ("http://www.google.com/ig/api?hl=zh_cn&weather=" + cityname);
InputStream InputStream = Url.openstream ();
String s, str; BufferedReader in = new BufferedReader (new InputStreamReader (InputStream, "GB2312"))//Save settings encoded as gb2312 stringbuffer StringBuffer = new Stri
Ngbuffer ();
Writer out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (FILEADDR), "UTF-8"));
while ((s = in.readline ())!= null) {stringbuffer.append (s);
str = new String (stringbuffer);
Out.write (str);
Out.close ();
In.close ();
catch (IOException e) {e.printstacktrace ();
//Read the required data file = new file (FILEADDR);
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
String str = NULL;
try {documentbuilder builder = Factory.newdocumentbuilder ();
Document doc = builder.parse (file);
NodeList Nodelist1 = (nodelist) doc. getElementsByTagName ("Forecast_conditions");
NodeList nodelist2 = Nodelist1.item (0). Getchildnodes (); str = Nodelist2.item (4). GetAttributes (). Item (0). Getnodevalue () + ", Temperature:" + nodelist2.item (1). GetAttributes (). Item (0). GetnodevaLue () + "℃-" + Nodelist2.item (2). GetAttributes (). Item (0). Getnodevalue () + "℃";
catch (Exception e) {e.printstacktrace ();
return str;
public static void Main (String args[]) {getgoogleweather GGW = new Getgoogleweather ();
String cityname = "Beijing";
String fileaddr = "f:/" + CityName + ". xml";
String temperature = Ggw.getweather (CityName, fileaddr);
Date nowdate = new Date ();
DateFormat DateFormat = Dateformat.getdateinstance ();
String today = Dateformat.format (nowdate);
SYSTEM.OUT.PRINTLN (Today + "" + cityname + "weather conditions are:" + temperature);
}
}