无法从XML中提取数据

时间:2013-04-13 18:44:29

标签: java xml jsoup

我使用getElementBytag方法从以下XML文档中提取数据(Yahoo finance news api http://finance.yahoo.com/rss/topfinstories


我使用以下代码。使用getelementsBytag方法获取新项目和标题没问题,但由于某些原因,在按标签搜索时不会选择链接。它只获取link元素的结束标记。这是XML文档的问题还是jsoup的问题?

import java.io.IOException;         
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;   

class GetNewsXML {
    /**
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String args[]){
        Document doc = null;
        String con = "http://finance.yahoo.com/rss/topfinstories";
        try {
            doc = Jsoup.connect(con).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Elements collection = doc.getElementsByTag("item");// Gets each news item
        for (Element c: collection){
            System.out.println(c.getElementsByTag("title"));
        }
        for (Element c: collection){
            System.out.println(c.getElementsByTag("link"));
        }
    }

1 个答案:

答案 0 :(得分:1)

你得到<link /> http://...;该链接将在<{em> link - 标记后作为文本节点。

但这不是问题:

final String url = "http://finance.yahoo.com/rss/topfinstories";

Document doc = Jsoup.connect(url).get();


for( Element item : doc.select("item") )
{
    final String title = item.select("title").first().text();
    final String description = item.select("description").first().text();
    final String link = item.select("link").first().nextSibling().toString();

    System.out.println(title);
    System.out.println(description);
    System.out.println(link);
    System.out.println("");
}

<强>解释

item.select("link")  // Select the 'link' element of the item
    .first()         // Retrieve the first Element found (since there's only one)
    .nextSibling()   // Get the next Sibling after the one found; its the TextNode with the real URL
    .toString()      // Get it as a String

使用您的链接,此示例将打印所有元素:

Tax Day Freebies and Deals
You made it through tax season. Reward yourself by taking advantage of some special deals on April 15.
http://us.rd.yahoo.com/finance/news/rss/story/SIG=14eetvku9/*http%3A//us.rd.yahoo.com/finance/news/topfinstories/SIG=12btdp321/*http%3A//finance.yahoo.com/news/tax-day-freebies-and-deals-133544366.html?l=1

(...)