dom4j没有加载整个xml

时间:2015-10-15 03:40:44

标签: java xml hibernate rss persistence

我正在做这个从Yahoo!下载天气的项目天气rss提供并将其写入数据库。雅虎的链接!天气: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;
    }

FYI,testBasicUsage()是一个将由JUnit运行的TestCase函数。

提前致谢!

1 个答案:

答案 0 :(得分:1)

您未在<yweather:astronomy>之后处理代码。其余数据位于<item><image>标记内:

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