【问题标题】:Cannot extract data from an XML无法从 XML 中提取数据
【发布时间】:2013-04-06 04:05:14
【问题描述】:

我使用 getElementBytag 方法从以下 XML 文档中提取数据(雅虎财经新闻 api http://finance.yahoo.com/rss/topfinstories


我正在使用以下代码。它使用 getelementsBytag 方法获取新项目和标题没有问题,但由于某种原因,在按标签搜索时不会拾取链接。它只获取链接元素的结束标签。是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"));
        }
    }

【问题讨论】:

  • 您提供的链接的页面来源是一个xml文档。检查以下帖子 stackoverflow.com/questions/9886531/how-to-parse-xml-with-jsoup 或使用 xml 解析器
  • 我可以解析其中所有标签的所有数据,但出于某种奇怪的原因, 标签的内容除外。我使用过 doc.select 和 getElementBytag。两者都适用于除 标签之外的所有其他标签。当我尝试获取 标记的内容时,我得到以下输出:
  • 基于我提供的链接,只需将 try .. catch 中的行替换为以下内容即可: doc = Jsoup.parse(new URL(con).openStream(), " ", Parser.xmlParser());
  • 您可以定义一个输入流变量,以便在操作完成后关闭它。

标签: java xml jsoup


【解决方案1】:

你得到<link /> http://...;链接放在之后 link-tag 作为文本节点。

但这不是问题:

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

(...)

【讨论】:

  • 谢谢,效果很好。我仍然不明白为什么您需要对链接使用 nextSibling() 方法。据我所见,链接标签中只有一个元素,因此 .first() 应该选择它。我在这里错过了什么吗?
  • 我不知道为什么,但是 jsoup 没有正确解析 <link> 标签。所以它不是<link>url here</link>,而是<link /> url here。链接标签为空,其链接在 之后。您可以将 webiste 解析为文档并打印 - 您会明白我的意思。但是,在我的示例中,我选择了(空)链接标签并使用nextSibling() 获取它后面的文本。需要first() 方法,因为select() 返回Elements 的实例(= Element 的列表)。
  • 干杯,这清除了它,谢谢。我想如果您有疑问,您应该始终将网站解析为文档并打印出来看看发生了什么。
  • @ollo 我参考了你的答案,并完美地得到了“链接”。谢谢你..很好的解释..请在stackoverflow.com/questions/17312544/…上查看我的问题。在那里,我只想要描述。
  • 友好提醒任何通过 jsoup 的人现在确实支持正确的 XML 解析(包括对 <link> 标签问题的修复)。见:stackoverflow.com/a/10158491/6425776
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 2021-03-18
相关资源
最近更新 更多