Reputation: 3536
I'm working on a weather app that shows the min and maximum temperature of a city using the code below.
The XML includes data for multiple days. To make it short I only included temp for 2 days. The code below pulls temperatures for the first date only, hence item(0). How do I make it pull the data out for the current date or any date? I've figured how to get current date but I'm not sure how to query the XML using that date.
//========================= Getting min/max Temperature ==================//
Node nNodem = docm.getElementsByTagName("temperature").item(0);
Element eElementm = (Element) nNodem;
String xmaxtemp = eElementm.getAttribute("max");
String xmintemp = eElementm.getAttribute("min");
//========================= Getting Current Date ========================//
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
String t = dft.format(Calendar.getInstance().getTime());
<weatherdata>
<location>
<name>London</name>
<type/>
<country>GB</country>
<timezone/>
<location altitude="0" latitude="51.50853" longitude="-0.12574" geobase="geonames" geobaseid="2643743" />
</location>
<credit/>
<meta>
<lastupdate/>
<calctime>0.0249</calctime>
<nextupdate/>
</meta>
<sun rise="2015-10-24T06:40:49" set="2015-10-24T16:47:34" />
<forecast>
<time day="2015-10-24">
<symbol number="500" name="light rain" var="10n" />
<precipitation/>
<windDirection deg="310" code="NW" name="Northwest" />
<windSpeed mps="4.72" name="Gentle Breeze" />
<temperature day="7.2" min="5.78" max="7.2" night="5.78" eve="7.2" morn="7.2" />
<pressure unit="hPa" value="1025.83" />
<humidity value="93" unit="%" />
<clouds value="clear sky" all="0" unit="%" />
</time>
<time day="2015-10-25">
<symbol number="802" name="scattered clouds" var="03d" />
<precipitation/>
<windDirection deg="216" code="SW" name="Southwest" />
<windSpeed mps="2.86" name="Light breeze" />
<temperature day="10.83" min="3.35" max="11.15" night="4.67" eve="7.03" morn="3.35" />
<pressure unit="hPa" value="1033.05" />
<humidity value="100" unit="%" />
<clouds value="scattered clouds" all="32" unit="%" />
</time>
</forecast>
</weatherdata>
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//time[@day='2015-10-26']";
try {
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node nNode = nodeList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Date: " + eElement.getAttribute("day"));
System.out.println("Max Temperature: " + eElement.getAttribute("max"));
}
}
} catch (XPathExpressionException e) {
System.out.println(e);
}
Shows this:
Date: 2015-10-26
Max Temperature:
Upvotes: 1
Views: 198
Reputation: 3536
I got it working using examples online. Here is the solution :)
XPath xPath = XPathFactory.newInstance().newXPath();
String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
String expression = "//time[@day='" + timeStamp + "']/temperature";
try {
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(docm, XPathConstants.NODESET);
Node nNodem = nodeList.item(0);
if (nNodem.getNodeType() == Node.ELEMENT_NODE) {
Element eElementm = (Element) nNodem;
double dmax = Math.round(Double.parseDouble(eElementm.getAttribute("max")));
int dxmax = (int) dmax;
xmaxtemp = Integer.toString(dxmax);
double dmin = Math.round(Double.parseDouble(eElementm.getAttribute("min")));
int dxmin = (int) dmin;
xmintemp = Integer.toString(dxmin);
}
} catch (Exception e) {
System.out.println(e);
}
Upvotes: 0
Reputation: 942
use and XMLPullParser to parse the xml and then iterate through the nodes to find what youre looking for
Upvotes: 1
Reputation: 2494
Create a list of Objects after parsing the XML from the NodeList
and then you can easily find any record as per the date.
Upvotes: 0