user5475558
user5475558

Reputation:

How access to inner same classname div with different idname in HTML data using Jsoup in android

I'm trying to parse data from HTML.I need to get the all names from inner div class=vacancy-item which has different idnames. Below please See the HTML code

<section class="home-vacancies" id="vacancy_wrapper">
<div class="home-block-title">job openings</div>
<div class="vacancy-filter">
    ...................
</div>
<div class="vacancy-wrapper">
    <div class="vacancy-item" data-id="9120">
        ..............
    </div>
    <div class="vacancy-item" data-id="9119">
        ..................
    </div>
    <div class="vacancy-item" data-id="9118">
        ................................
    </div>
    <div class="vacancy-item" data-id="9117">
        .............................
    </div>

Here is my code: Please help.

       doc = Jsoup.connect("URL").get();
       //title = doc.select(".page-content div:eq(3)");
       title = doc.getElementsByClass("div[class=vacancy-wrapper]");
       titleList.clear();
       for (Element titles : title) {     
            String text = titles.getElementsB("vacancy-item").text();
            titleList.add(text);
       }

Thanks!

Upvotes: 1

Views: 835

Answers (1)

nyname00
nyname00

Reputation: 2566

You can only query for a class attribute with getElementByClass, e.g. getElementByClass("vacancy-wrapper") would work.

You will also need a second loop to get each vacancy-items text as a separate element:

Elements title = doc.getElementsByClass("vacancy-wrapper");
for (Element titles : title) {
    Elements items = titles.getElementsByClass("vacancy-item");
    for (Element item : items) {
        String text = item.text();
        // process text
    }
}

An other option would be to use Jsoup's select method:

Elements es = doc.select("div.vacancy-wrapper div.vacancy-item");
for (Element vi : es) {
    String text = vi.text());
    // process text
}

This would select all div elements with a class attribute vacancy-item that are under a div with a class attribute vacancy-wrapper.

Upvotes: 2

Related Questions