dom4j 未加载整个 xml
dom4j not loading through entire xml
我正在做这个从 Yahoo! 下载天气的项目。天气 rss 提要并将其写入数据库。指向 Yahoo! 的链接天气: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() 是一个 TestCase 函数,将由 JUnit 运行。
提前致谢!
您没有在 <yweather:astronomy>
之后处理标签。其余数据在 <item>
和 <image>
标签内:
else if (child.getQualifiedName().equals("item")){
String title = child.elementText("title");
System.out.println("title: " + title);
}
我正在做这个从 Yahoo! 下载天气的项目。天气 rss 提要并将其写入数据库。指向 Yahoo! 的链接天气: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() 是一个 TestCase 函数,将由 JUnit 运行。
提前致谢!
您没有在 <yweather:astronomy>
之后处理标签。其余数据在 <item>
和 <image>
标签内:
else if (child.getQualifiedName().equals("item")){
String title = child.elementText("title");
System.out.println("title: " + title);
}