java - dom4j 未加载整个 xml

标签 java xml hibernate rss persistence

我正在做这个项目,从雅虎下载天气!天气 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/

相关文章:

Java 数据结构/C++ STL 等价物?

java - Gradle 和 Android : Could not resolve dependencies com. android.support

java - 如何添加标题?

java - 如何让 Hibernate 在运行时而不是在编译时删除并重新创建数据库?

Java,文件不存在错误,即使它存在,网络服务器

java - 通过不同计算机上的套接字发送大文件

android - 为什么不为 Check Box 使用重力中心

java - 我可以通过编程方式将 TextView 的 xml 文本与另一个字符串进行比较吗?

java - Criteria API 和独特的结果

postgresql - 推迟物化 View 的创建