问题描述
我正在使用一个气象应用程序,该应用程序使用以下代码显示城市的最低和最高温度。
XML包含多天的数据。 为了简短起见,我只包括了2天的临时工。 下面的代码仅在第一个日期拉温度,因此是item(0) 。 如何使它提取当前日期或任何日期的数据 ? 我已经找到了如何获取当前日期,但不确定如何使用该日期查询XML。
//========================= 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); }
显示此:
Date: 2015-10-26 Max Temperature:
1楼
使用和XMLPullParser解析xml,然后遍历节点以查找您要寻找的内容
2楼
从NodeList解析XML后创建对象列表
然后您就可以轻松找到该日期的任何记录。
3楼
我通过在线示例使它工作。 这是解决方案:)
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); }