Search code examples
javajsoup

-Solved- Extracting Text after a tag using Jsoup


Given below code gives me output something like this,

<a href="https://timesofindia.indiatimes.com/india/uk-envoy-lays-wreath-at-jallianwala-bagh-memorial-expresses-deep-regret/articleshow/68860078.cms"><img border="0" hspace="10" align="left" style="margin-top:3px;margin-right:5px;" src="https://timesofindia.indiatimes.com/photo/68860078.cms" /></a>British High Commissioner to India Sir Dominic Asquith laid a wreath at the Jallianwala Bagh memorial here on Saturday on the centenary of the massacre and said Britain "deeply regretted" the suffering caused to the victims.

I'm trying to extract the text after </a> this tag

This is my code is there any method in jsoup that do the part or something else I'm missing?

try {
            Document document = Jsoup.connect("https://timesofindia.indiatimes.com/rssfeeds/-2128936835.cms").parser(Parser.xmlParser()).get();
            Elements items = document.getElementsByTag("item");
            for (Element element : items) {
                String title = element.select("title").text();
                String link = element.select("link").text();
                String time = element.select("pubDate").text();
                String description = element.select("description").text();
            System.out.println(description);
            }
        } catch (IOException ex) {
            Logger.getLogger(TimesOfIndia.class.getName()).log(Level.SEVERE, null, ex);
        }

Expected output: British High Commissioner to India Sir Dominic Asquith laid a wreath at the Jallianwala Bagh memorial here on Saturday on the centenary of the massacre and said Britain "deeply regretted" the suffering caused to the victims.

Output: <a href="https://timesofindia.indiatimes.com/india/uk-envoy-lays-wreath-at-jallianwala-bagh-memorial-expresses-deep-regret/articleshow/68860078.cms"><img border="0" hspace="10" align="left" style="margin-top:3px;margin-right:5px;" src="https://timesofindia.indiatimes.com/photo/68860078.cms" /></a>British High Commissioner to India Sir Dominic Asquith laid a wreath at the Jallianwala Bagh memorial here on Saturday on the centenary of the massacre and said Britain "deeply regretted" the suffering caused to the victims.


Solution

  • Element has the nextSibling() method, which should work:

    element.select("description").select("a").nextSibling().text();