SeasonalShot
SeasonalShot

Reputation: 2579

Choose a div class inside a div class using jsoup

<div class="main-banner">
<div class="row" style="height:298px;">



<div class="row" style="margin-top:40px;">
    <div class="seven columns">
        some other data,div and css blocks....
        </div>
    </div>

</div>

</div>
</div>

To select the top div i can use

    Element title = doc.select("div.productTitle").first();
  1. I need to select the 4th div (class='seven columns') and display the data in a webview, so what's the syntax for that?

  2. Inside another inner level div, there are some <select> <options> pairs.How can i get it into an Array using jsoup?

Upvotes: 0

Views: 4077

Answers (3)

Jonathan Hedley
Jonathan Hedley

Reputation: 10522

You can use try.jsoup.org to help test and debug your HTML and selectors.

Upvotes: 0

RAJAT TRIVEDI
RAJAT TRIVEDI

Reputation: 1

use this site to get your syntax http://jsoup.org/cookbook/extracting-data/selector-syntax and if you do not get your result let me know.. and here is an example how mto pasre data using jsoup

    public class ListShow extends Activity {
String url;
String DetailText;
TextView tv1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.textView1);

    try {
        Document doc = Jsoup.connect("http://www.srmcem.ac.in")
                .timeout(10000).get();

        Elements link = doc
                .select(".sidebar > ul > li > div > marquee > a[href]");
        String url1 = "";
        String text = "";
        String detail = "";
        for (Element element : link) {
            text = element.text();

            if (element != null) {
                url1 = element.absUrl("href");
            }
            detail += text + "\n" + url1 + "\n\n";
        }

        DetailText = detail.toString();
    } catch (Exception e) {
        // TODO: handle exception
    }
    tv1.setText(DetailText);

}
}

Upvotes: 0

Daniel B
Daniel B

Reputation: 8879

I do not clearly understand your question. Could you be more specific? You know how to select a div, so why don't you?

Elements div = doc.select("div.ClassName");

If you have a structure that looks like this, and want to select the fourth one:

<div class="row">
    <div>Number one!</div>
    <div>Number two!</div>
    <div>Number three!</div>
    <div>Number four!</div>
    <div>Number five!</div>
</div>

You can use:

Element fourthDiv = doc.select("div.row div:eq(4)");

This way you can nest selections quite easily!

Upvotes: 1

Related Questions