-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfo.java
More file actions
74 lines (65 loc) · 2.34 KB
/
Info.java
File metadata and controls
74 lines (65 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package ass;
import java.io.InputStream;
import java.net.URL;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Info {
static String hum, temp, temH, temL, loc, cond;
private static Document doc;
private static final int locCode=2356940;
private static DocumentBuilderFactory domF = DocumentBuilderFactory.newInstance();
private static DocumentBuilder bdr;
public static void Update(){
try{
URL xURL = new URL("https://query.yahooapis.com/v1/public/yql?=xml&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27f%27%20AND%20woeid%20=%20%27"+locCode+"%27");//use yahoo search query instead of apis to grab xml then parse l8r
InputStream iS = xURL.openStream();
pLxml(iS);
} catch (Exception ex){
System.err.println("XML fucked up sorry");
System.exit(1);
}
Node n; Element e;
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("results");
for (int i = 0; i < nList.getLength(); i++) {
if (nList.item(i).getNodeType()!=Node.ELEMENT_NODE){
continue;
} else {
try{
n = ((Element) nList.item(i)).getElementsByTagName("yweather:location").item(0);
e = (Element) n;
loc = e.getAttribute("city");
loc +=",";
loc += e.getAttribute("region");
n = ((Element) nList.item(i)).getElementsByTagName("yweather:condition").item(0);
e = (Element) n;
temp=e.getAttribute("temp");
cond=e.getAttribute("text");
n = ((Element) nList.item(i)).getElementsByTagName("yweather:forecast").item(0);
e = (Element) n;
temH=e.getAttribute("high");
temL=e.getAttribute("low");
n = ((Element) nList.item(i)).getElementsByTagName("yweather:atmosphere").item(0);
e = (Element) n;
hum = e.getAttribute("humidity");
} catch (Exception ex){
}
}//endif
}//edn4
}
private static void pLxml(InputStream iS) {//parse and loads xml
try {
bdr = domF.newDocumentBuilder();
domF.setValidating(false);
domF.setNamespaceAware(false);
doc=null;
doc = bdr.parse(iS);
} catch (Exception ex) {
System.err.println("unable to load XML: " + ex);
}
}
}