从XML搜索和显示值 - Android Java

时间:2015-10-24 22:07:15

标签: java android xml xpath

我正在开发一个天气应用程序,使用下面的代码显示城市的 min 最大温度。

XML包含多天的数据。为了缩短它我只包括临时2天。下面的代码将第一个日期的温度拉,因此项目(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: 

3 个答案:

答案 0 :(得分:1)

使用和XMLPullParser来解析xml,然后遍历节点以找到你要找的东西

答案 1 :(得分:0)

从NodeList解析XML后创建一个对象列表

然后您可以根据日期轻松找到任何记录。

答案 2 :(得分:0)

我在线使用示例。这是解决方案:)

&#13;
&#13;
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);
}
&#13;
&#13;
&#13;