gpt4 book ai didi

java - 如何从标签名称更改的 xml 中获取 api 数据

转载 作者:行者123 更新时间:2023-12-02 04:30:31 24 4
gpt4 key购买 nike

所以我尝试使用 openweathermap api 进行五天的天气预报。它返回五天天气预报的 xml here

我一直在尝试使用代码 NodeList nodeList = doc.getElementsByTagName("time"); 获取信息。如果您检查 xml,您将看到标签 <time>包含每 2 小时范围的预测。但问题是我似乎无法从这些标签中获取任何内容,因为名称实际上是 <tag="date range""time range" to "time range + 3hrs">

 try {
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(this.fiveDayForecastURL);
doc.getDocumentElement().normalize();

NodeList nodeList = doc.getElementsByTagName("time");
for (int i = 0; i < nodeList.getLength(); i++){
Node node = nodeList.item(i);
NamedNodeMap namedNodeMap = node.getAttributes();
Node attr = namedNodeMap.getNamedItem("max");
// just trying to grab anything from inside these tags
// but ideally would want min and max temp for the range
if (node.getNodeType() == Node.ELEMENT_NODE){
System.out.println(attr);
// always prints [time: null]
}
System.out.println(node);
// always prints null
}
} catch (ParserConfigurationException | IOException ex) {
ex.printStackTrace();
} catch (org.xml.sax.SAXException e) {
e.printStackTrace();
}

我确定我遗漏了一些代码行或其他内容,但是有没有办法获取时间标签之间的所有内容,即使标签名称每次都会更改?谢谢

最佳答案

首先,我忘记了用 DOM 解析器解析 XML 是多么麻烦。

您考虑过requesting the returned data as JSON然后您可以使用 gson 进行解析?

所以 - 您的方向是正确的,但为了获得给定时间段内的最低/最高温度,您需要继续深入 DOM 层次结构。

温度是时间的子元素,因此您需要获取它,然后从中获取最小和最大属性值。

类似于:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;

public class Test {
private static final String FIVE_DAY_FORECAST_URL =
"https://api.openweathermap.org/data/2.5/forecast?q=Denver&appid=8984d739fa91d7031fff0e84a3d2c520&mode=xml&units=imperial";

private static final String TIME_ELEM = "time";
private static final String TEMPERATURE_ELEM = "temperature";
private static final String TIME_FROM_ATTR = "from";
private static final String TIME_TO_ATTR = "to";
private static final String TEMPERATURE_MIN_ATTR = "min";
private static final String TEMPERATURE_MAX_ATTR = "max";

private static void getWeatherForcast() {
try {
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
final Document doc = dBuilder.parse(FIVE_DAY_FORECAST_URL);
doc.getDocumentElement().normalize();

NodeList nodeList = doc.getElementsByTagName(TIME_ELEM);
for (int i = 0; i < nodeList.getLength(); i++) {
final Node node = nodeList.item(i);
final NamedNodeMap namedNodeMap = node.getAttributes();
final Node fromAttr = namedNodeMap.getNamedItem(TIME_FROM_ATTR);
final Node toAttr = namedNodeMap.getNamedItem(TIME_TO_ATTR);
System.out.println("Time: " + fromAttr + " " + toAttr);
final NodeList timeChildren = node.getChildNodes();
for (int j = 0; j < timeChildren.getLength(); j++) {
final Node timeChild = timeChildren.item(j);
if (TEMPERATURE_ELEM.equals(timeChild.getNodeName())) {
final NamedNodeMap temperatureAttrMap = timeChild.getAttributes();
final String minTemp = temperatureAttrMap.getNamedItem(TEMPERATURE_MIN_ATTR).getNodeValue();
final String maxTemp = temperatureAttrMap.getNamedItem(TEMPERATURE_MAX_ATTR).getNodeValue();
System.out.println("min: " + minTemp + " max: " + maxTemp);
}
}

}
} catch (ParserConfigurationException | IOException ex) {
ex.printStackTrace();
} catch (org.xml.sax.SAXException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
getWeatherForcast();
}
}

关于java - 如何从标签名称更改的 xml 中获取 api 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56589237/

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