J4C3N-14
J4C3N-14

Reputation: 696

How do you get a div class inside a div class inside a div class?

My problem is that I need to get a div class inside a div class inside a div class and the are 4 instances of the classes with the same name but different data... I can currently get the first div class inside the div class but i need to be able to access other elements within it aswell......for example:

docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
Elements tideTableRows = docTide.select("div.tide_row.odd");
Element firstDiv = tideTableRows.first();
Element secondDiv = tideTableRows.get(1);
System.out.println("This is the first div: " + firstDiv.text());
System.out.println("This is the second div: " + secondDiv.text()); 

but this is the structure of the webpage where there are 2 repeats and I need to access each of them e.g:

 <div class="tide_row odd"> 
  <div class="time">00:57</div>
  <div class="height_m">4.9</div>
  <div class="height_f">16,1</div>
  <div class="range_m">1.9</div>
  <div class="range_f">6,3</div>
  </div>
 <div class="tide_row even">
  <div class="time">07:23</div>
  <div class="height_m">2.9</div>
  <div class="height_f">9,6</div>
  <div class="range_m">2</div>
  <div class="range_f">6,7</div>
  </div>
  <div class="tide_row odd">
  <div class="time">13:46</div>
  <div class="height_m">5.1</div>
  <div class="height_f">16,9</div>
  <div class="range_m">2.2</div>
  <div class="range_f">7,3</div>
  </div>
  <div class="tide_row even">
  <div class="time">20:23</div>
  <div class="height_m">2.8</div>
  <div class="height_f">9,2</div>
  <div class="range_m">2.3</div>
  <div class="range_f">7,7</div>
  </div>

So basically it has nested classes in separate classes with the same name, how can I construct the correct syntax to return the data from the classes separately? This is quite hard to explain!

Edit: This is how I managed to extract the information from the nested classes:

 docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
                         Elements tideTimeOdd = docTide.select("div.tide_row.odd div:eq(0)");
                         Elements tideTimeEven = docTide.select("div.tide_row.even div:eq(0)");
                         Elements tideHightOdd = docTide.select("div.tide_row.odd div:eq(2)");
                         Elements tideHightEven = docTide.select("div.tide_row.even div:eq(2)");
                            Element firstTideTime = tideTimeOdd.first();
                            Element secondTideTime = tideTimeEven.first();
                            Element thirdTideTime = tideTimeOdd.get(1);
                            Element fourthTideTime = tideTimeEven.get(1);

                            Element firstTideHight = tideHightOdd.first();
                            Element secondTideHight = tideHightEven.first();
                            Element thirdTideHight = tideHightOdd.get(1);
                            Element fourthTideHight = tideHightEven.get(1);

Upvotes: 0

Views: 285

Answers (2)

superuser
superuser

Reputation: 731

It would work fine by just doing:

 docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
                            Elements tideTableRows = docTide.select("div[class=tide_row odd]");
                            Element firstDiv += tideTableRows.select("div[class=time]");
                            Element secondDiv += tideTableRows.select("div[class=height]");

You should try to access elements by id if you can. It makes your code a lot simpler and if you have 50 headers in the same container, as an example, this way you don't have to count them all.

Seperate elements:

 docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
                            Element tideTableRows = docTide.select("div[class=tide_row odd]").first();
                            Element firstDiv1 = tideTableRows.select("div[class=time]");
                            Element secondDiv1 = tideTableRows.select("div[class=height]");
                            tideTableRows2 = docTide.select("div[class=tide_row odd]").second();
                            Element firstDiv2 = tideTableRows.select("div[class=time]");
                            Element secondDiv2 = tideTableRows.select("div[class=height]");

Upvotes: 1

luksch
luksch

Reputation: 11712

You can try this:

docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
Elements tideTableRows = docTide.select("div.tide_row");
for (Element tideTableRow : tideTableRows){
  if (tideTableRow.hasClass("odd")){
     //do the odd stuff...
  }
  Elements innerDivs = tideTableRows.select("div");

  for (Element innerDiv : innerDivs){
    //do whatever you need
  }
}

Note: The code is not tested.

Update: I showed you how to access the odd rows only... From there you should be able to get the rest by yourself I hope.

Upvotes: 1

Related Questions