gpt4 book ai didi

java - dom4j 未加载整个 xml

转载 作者:行者123 更新时间:2023-12-01 11:03:57 25 4
gpt4 key购买 nike

我正在做这个项目,从雅虎下载天气!天气 rss feed 并将其写入数据库。雅虎的链接!天气:http://weather.yahooapis.com/forecastrss?p=95129现在的问题是它没有加载到标签中。它可以读取所有其他部分,但在天文标签之后它会停止。谁能告诉我为什么这不起作用? (另外,我正在使用 Hibernate 和 JPA)

这是我的代码:

public void testBasicUsage() {
URL myURL;
Document document;
Element root;
Weather weather = new Weather();
try {
myURL = new URL("http://weather.yahooapis.com/forecastrss?p=95129");
document = parse(myURL);
root = document.getRootElement();
Element row;
Iterator itr;
for (Iterator i = root.elementIterator(); i.hasNext();) {
row = (Element) i.next();
itr = row.elementIterator();
while (itr.hasNext()) {
Element child = (Element) itr.next();
if(child.getQualifiedName().equals("yweather:location")){
String location = child.attributeValue("city") + ", " +
child.attributeValue("region");
weather.setLocation(location);
System.out.println("location: " + location);
}else if(child.getQualifiedName().equals("yweather:wind")){
String chill = child.attributeValue("chill");
int direction = Integer.parseInt(child.attributeValue("direction"));
int speed = Integer.parseInt(child.attributeValue("speed"));
Wind wind = new Wind(chill,direction,speed);
weather.setWind(wind);
System.out.println("chill: " + chill + "; direction: " + direction + "; speed: " + speed);
}else if(child.getQualifiedName().equals("yweather:atmosphere")){
int humidity = Integer.parseInt(child.attributeValue("humidity"));
int visibility = Integer.parseInt(child.attributeValue("visibility"));
double pressure = Double.parseDouble(child.attributeValue("pressure"));
Atmosphere atmosphere = new Atmosphere(humidity,visibility,pressure);
weather.setAtmosphere(atmosphere);
System.out.println("humidity: " + humidity + "; visibility: " + visibility + "; pressure: " + pressure);
}else if(child.getQualifiedName().equals("yweather:astronomy")){
String sunrise = child.attributeValue("sunrise");
String sunset = child.attributeValue("sunset");
Astronomy astronomy = new Astronomy(sunrise,sunset);
weather.setAstronomy(astronomy);
System.out.println("sunrise: " + sunrise + "; sunset: " + sunset);
}else if(child.getQualifiedName().equals("yweather:condition")){
String text = child.attributeValue("text"); // condition text
int code = Integer.parseInt(child.attributeValue("code"));
int temp = Integer.parseInt(child.attributeValue("temp"));
String date = child.attributeValue("date");
Condition condition = new Condition(text,code,temp,date);
weather.setCondition(condition);
System.out.println("text: " + text + "; temp: " + temp + "; date: " + date);
}
}
}
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(weather);
entityManager.getTransaction().commit();
entityManager.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Document parse(URL url) throws DocumentException
{
SAXReader reader = new SAXReader();
Document document = reader.read(url);
return document;
}

仅供引用,testBasicUsage() 是一个将由 JUnit 运行的 TestCase 函数。

提前致谢!

最佳答案

您没有处理 <yweather:astronomy> 之后的标签。其余数据在<item>里面和<image>标签:

else if (child.getQualifiedName().equals("item")){
String title = child.elementText("title");
System.out.println("title: " + title);
}

关于java - dom4j 未加载整个 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139471/

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