简体   繁体   English

如何从标签中检索数据<content:encoded>使用来自 RSS 提要的 CDATA 和用于 android 的 Jsoup?</content:encoded>

[英]How to retrieve data from tag <content:encoded> with CDATA from RSS feed with Jsoup for android?

I want to retrieve data from an RSS feed using jsoup.我想使用 jsoup 从 RSS 提要中检索数据。 I am able in all tag but I can't do It when there is content:encoded tag.我可以使用所有标签,但是当有 content:encoded 标签时我不能这样做。 please anyone help me how to get data from content:encoded tag.请任何人帮助我如何从内容获取数据:编码标签。 My feed URL is https://sambad.in/feed/ and my code is as well Document doc = Jsoup.parse(String.valueOf(response));我的提要 URL 是https://sambad.in/feed/我的代码也是 Document doc = Jsoup.parse(String.valueOf(response)); Elements itemElements = doc.select("item");元素 itemElements = doc.select("item");

            for (int i = 0; i < itemElements.size(); i++) {

                Element item = itemElements.get(i);
                String title = item.child(0).text();
                String link=item.child(1).text();
                String imgUrl=extractImageUrl(item.select("description").text());
                String description = extractPostText(item.select("description").text())+"From Sambad: By Pin2";
                String fullnews=extractPostText(item.children().select("http://purl.org/rss/1.0/modules/content/encoded").text());

The selector to use will be content|encoded .要使用的选择器将是content|encoded To specify a namespaced tag, replace the : with a |要指定命名空间标记,请将:替换为| . . See the jsoup selector documentation for more examples.有关更多示例,请参阅jsoup 选择器文档

Here is a live example on Try Jsoup .这是Try Jsoup上的一个活生生的例子。

A couple points to note:有几点需要注意:

  1. For RSS, you should use the XML parser instead of the (default) parser.对于 RSS,您应该使用XML 解析器而不是(默认)解析器。 Normally that would happen automatically if you were using Jsoup.connect(url) to load the content, as it sets the parser based on the content type.通常,如果您使用Jsoup.connect(url)加载内容,这会自动发生,因为它会根据内容类型设置解析器。 But you are bypassing that (by supplying a String input), so you need to specify it manually.但是您正在绕过它(通过提供字符串输入),因此您需要手动指定它。
  2. The result of the content|encoded selector will be a set of Elements containing text with HTML tags (not parsed HTML elements). content|encoded选择器的结果将是一组包含带有 HTML 标记的文本的元素(未解析的 HTML 元素)。 That's because the content in the RSS is HTML encoded (escaped).这是因为 RSS 中的内容是 HTML 编码(转义)的。 If you want it as parsed HTML, you should next use the Jsoup.parseBodyFragment(String) method on the text.如果您希望它解析为 HTML,您接下来应该对文本使用Jsoup.parseBodyFragment(String)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM