Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

java - Jsoup selector on RSS <link> tag returns empty string with .text() method

I'm using to parse an feed using . I'm having problems getting a result when trying to select the first <link> element in the document.

When I use title.text() I get an expected result with this code:

Document doc = Jsoup.connect(BLOG_URL).get();
Element title = doc.select("rss channel title").first();
System.out.println(title.text()); // print the blog title...

However, link.text() doesn't work the same way:

Element link = doc.select("rss channel link").first();
System.out.println(link.text()); // prints empty string

When I inspect doc.select("rss channel link") the Element link object is populated but the .println() statement is just an empty string.

What makes .select("rss channel link") so dang special that I can't figure out how to use it?

Edit: The RSS response begins like this:

   <?xml version="1.0" encoding="UTF-8"?>
    <rss>
    <channel>
    <title>The Blog Title</title>
    <link>http://www.the.blog/category</link>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your rss feed is XML, not HTML. For this to work, you must tell JSoup to use its XMLParser. This will work:

String rss = "<?xml version="1.0" encoding="UTF-8"?>"
  +"<rss><channel>"
  +  "<title>The Blog Title</title>"
  +  "<link>http://www.the.blog/category</link>"
  +"</channel></rss>";

Document doc = Jsoup.parse(rss, "", Parser.xmlParser());

Element link = doc.select("rss channel link").first();
System.out.println(link.text()); // prints empty string

Explanation:

The link tag in HTML follows a different format and Jsoup tries to interpret the <link> of your rss as such html tag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...