Rabindra Nath Nandi Rabindra Nath Nandi - 1 month ago 6
HTML Question

How to extract the next tag elements in jsoup

I am using jsoup to parse html document . I need the value of P tag just after the SPAN tag which contains id attribute.

I am trying with the following code

Elements spanList = body.select("span");
if (spanList != null) {
for (Element element1 : spanList) {
if (element1.attr("id").contains("midArticle")) {
Element element = element1.after("<p>"); // This line is wrong
if (element != null) {
String text = element.text();
if (text != null && !text.isEmpty()) {
out.println(text);
}
}
}
}
}


The html sample code

<span id="midArticle_9"></span>
<p>"The Director owes it to the American people to immediately provide the full details of what he is now examining," Podesta said in a statement. "We are confident this will not produce any conclusions different from the one the FBI reached in July." </p>
<span id="midArticle_10"></span>
<p>Clinton has repeatedly apologized for using the private email server in her home instead of a government email account for her work as secretary of state from 2009 to 2013. She has said she did not knowingly send or receive classified information.</p>

Answer

i hope this resolves your issue...

public static void main(String[] args) {
        String html = "<span id=\"midArticle_9\"></span><p>\"The Director owes it to the American people to immediately provide the full details of what he is now examining,\" Podesta said in a statement. \"We are confident this will not produce any conclusions different from the one the FBI reached in July.\" </p><span id=\"midArticle_10\"></span><p>Clinton has repeatedly apologized for using the private email server in her home instead of a government email account for her work as secretary of state from 2009 to 2013. She has said she did not knowingly send or receive classified information.</p>";
        Document document = Jsoup.parse(html);
        Elements elements = document.getElementsByTag("span");
        for (Element element : elements) {
            System.out.println(element.nextElementSibling().text());
        }
}