gpt4 book ai didi

java - 从 XML 中搜索和显示值 - Android Java

转载 作者:行者123 更新时间:2023-11-30 01:49:04 25 4
gpt4 key购买 nike

我正在开发一个天气应用程序,该应用程序使用以下代码显示城市的最低最高 温度。

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:

最佳答案

使用和 XMLPullParser 解析 xml,然后遍历节点以找到您要查找的内容

关于java - 从 XML 中搜索和显示值 - Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com