Geek
Geek

Reputation: 3329

Getting text from Div tags

I have a main Div tag with multiple div tags as below. The child Div tags have no class/id that distinguishes from the other child div tags. Now I want to extract the text value from the 2nd child Div tag. How can i do that?

<div class="logFor" style="position: relative; height: 101px; padding: 5px;">
     <div style="color: #6b6b6b; font-weight: bold;">This is a monster</div>
     <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">Monster in Black</div>
     <div style="position: absolute; left: 5px; bottom: 0;">
     <div style="position: absolute; right: 5px; bottom: 0;">
</div>

I want to get the text "Monster in Black". This Div doesnt have an id/name and not sure if this style would be same or change. How would i extract using jSoup?

Upvotes: 0

Views: 2837

Answers (3)

hakyer
hakyer

Reputation: 454

You can achieve that with following code:

Document doc = Jsoup.parse(new File("test.html"), "utf-8");
Elements select = doc.select("div > div:eq(1)");
System.out.println(select.text());

Also check out this javadoc for details about Selector

Upvotes: 3

Paul Grime
Paul Grime

Reputation: 15104

package stackoverflow;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JSoupTest {
    public static void main(String[] args) throws IOException {
        InputStream in = JSoupTest.class.getResourceAsStream("JSoupTest.txt");

        String html = IOUtils.toString(in);

        Document doc = Jsoup.parse(html);

        Elements divs = doc.select("DIV");
        System.out.println(divs);

        Element div = divs.get(2);
        System.out.println("Monster in Black".equals(div.text()));
    }
}

Produces:

<div class="logFor" style="position: relative; height: 101px; padding: 5px;"> 
 <div style="color: #6b6b6b; font-weight: bold;">
  This is a monster
 </div> 
 <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">
  Monster in Black
 </div> 
 <div style="position: absolute; left: 5px; bottom: 0;"> 
  <div style="position: absolute; right: 5px; bottom: 0;"> 
  </div> 
 </div>
</div>
<div style="color: #6b6b6b; font-weight: bold;">
 This is a monster
</div>
<div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">
 Monster in Black
</div>
<div style="position: absolute; left: 5px; bottom: 0;"> 
 <div style="position: absolute; right: 5px; bottom: 0;"> 
 </div> 
</div>
<div style="position: absolute; right: 5px; bottom: 0;"> 
</div>
true

Upvotes: 1

HainKurt
HainKurt

Reputation: 1

use jquery

<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" ></script>
<script>
$(document).ready(function() {
    alert($(".logFor div:nth-child(3)").html());
});
</script>
<body>
<div class="logFor" style="position: relative; height: 101px; padding: 5px;">
     <div style="color: #6b6b6b; font-weight: bold;">This is a monster</div>
     <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">Monster in Black</div>
     <div style="position: absolute; left: 5px; bottom: 0;">HainKurt</div>
     <div style="position: absolute; right: 5px; bottom: 0;">Just joined to SO!</div>
</div>
</body>
</html>

Upvotes: 0

Related Questions