Jsoup。选择返回空值,而元素则包含文本 [英] Jsoup .select returns empty value but element does contains text

查看:683
本文介绍了Jsoup。选择返回空值,而元素则包含文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个XML链接标签元素的文本: http://www.istana.gov.sg/latestupdate/rss.xml

I'm trying to get the text of "link" tag element in this xml: http://www.istana.gov.sg/latestupdate/rss.xml

我有codeD获得的第一篇文章。

I have coded to get the first article.

        URL = getResources().getString(R.string.istana_home_page_rss_xml);
        // URL = "http://www.istana.gov.sg/latestupdate/rss.xml";

        try {
            doc = Jsoup.connect(URL).ignoreContentType(true).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // retrieve the link of the article
        links = doc.select("link");

        // retrieve the publish date of the article
        dates = doc.select("pubDate");

        //retrieve the title of the article
        titles = doc.select("title");

        String[] article1 = new String[3];
        article1[0] = links.get(1).text();
        article1[1] = titles.get(1).text();
        article1[2] = dates.get(0).text();

文章出来很好,但链接返回值(整个整个链路的元素返回,值)。标题和日期没有任何问题。链接标签包含一个URL文本。任何人都知道为什么它返回价值?

The article comes out nicely but the link returns "" value (The whole entire link elements return "" value). The titles and dates have no problems. The link tag consist of a URL text. Anyone knows why it returns "" value?

推荐答案

它看起来像默认的HTML解析器无法识别<链接> 作为有效的标签,并自动为关闭它<链接/方式> 这意味着,这个标签的内容是空的。

It looks like default HTML parser can't recognize <link> as valid tag and is automatically closing it <link /> which means that content of this tag is empty.

要解决这个问题,而不是HTML解析器可以使用XML解析器不很在乎标签名称。

To solve this problem instead of HTML parser you can use XML parser which doesn't care that much about tag names.

doc = Jsoup.connect(URL)
      .ignoreContentType(true)
      .parser(Parser.xmlParser()) // <-- add this
      .get();

这篇关于Jsoup。选择返回空值,而元素则包含文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆