简体   繁体   中英

how to retrieve data from strong tags in html file using jsoup?

I have some html data like

<div class="bs-example">
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">ABC</h3>
    </div>
    <div class="panel-body">
      <div class="slimScroller" style="height:280px; position: relative;" data-rail-visible="1" data-always-visible="1">
        <strong>Name:</strong>
        <a href="https://ABC"> </a><br />
        <strong>ID No:</strong> XXXXX<br />
        <strong>Status:</strong> ACTIVE<br />
        <strong>Class:</strong> 5<br />
        <strong>Category:</strong> A<br />
        <strong>Marks:</strong> 500<br />
      </div>
    </div>
  </div>
</div>

I want output as (multiple students data):

 Name: ABC
 ID No.: XXXXX
 Status: Active
 Class: 5
 Category: A
 Marks: 500

How to get this data using jsoup or any other way? Please help.

You can use Element.nextElementSibling() or/and Element.nextSibling() to get the output you need.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Exam {
  public static void main(String[] args) {
    String html =  "<div class=\"bs-example\">" +
                    "  <div class=\"panel panel-primary\">" +
                    "    <div class=\"panel-heading\">" +
                    "      <h3 class=\"panel-title\">ABC</h3>" +
                    "    </div>" +
                    "    <div class=\"panel-body\">" +
                    "      <div class=\"slimScroller\" style=\"height:280px; position: relative;\" data-rail-visible=\"1\" data-always-visible=\"1\">" +
                    "        <strong>Name:</strong>" +
                    "        <a href=\"https://ABC\"> </a><br />" +
                    "        <strong>ID No:</strong> XXXXX<br />" +
                    "        <strong>Status:</strong> ACTIVE<br />" +
                    "        <strong>Class:</strong> 5<br />" +
                    "        <strong>Category:</strong> A<br />" +
                    "        <strong>Marks:</strong> 500<br />" +
                    "      </div>" +
                    "    </div>" +
                    "  </div>" +
                    "</div>";

    Document doc = Jsoup.parse(html);
    Elements eles = doc.select("div.slimScroller strong");
    for(Element e :eles)
    System.out.println(e.text() +
                       ( e.nextElementSibling().tagName().equals("a")? 
                         e.nextElementSibling().attr("href").replace("https://", ""):
                         e.nextSibling().toString()));
  }
}

The following code should provide the output specified based off your comment describing how your a tags are:

private static void printStudentInfo(Document document){
    Elements students = document.select("div.slimScroller strong");

    for(Element student : students){
        System.out.print(student.text());

        System.out.println(student.nextElementSibling().tagName().equals("a") ?
                student.nextElementSibling().text() : student.nextSibling().toString());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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