1
votes

I want to extract HTML code from a div element using jsoup HTML parser library.

HTML code:

<div class="entry-content">
   <div class="entry-body">
      <p><strong>Text 1</strong></p>
      <p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset  asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519" /></a><br /></strong></p>
      <p><em>Text 2</em> </p>
   </div>
</div>

Extract part:

String content = ... the content of the HTML from above
Document doc = Jsoup.parse(content);
Element el = doc.select("div.entry-body").first();

I want to the result el.html() to be the whole HTML from div tab entry-body:

<p><strong>Text 1</strong></p>
  <p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset  asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519" /></a><br /></strong></p>
  <p><em>Text 2</em> </p>

but I get only the first <p> tag:

<p><strong>Text 1</strong></p>
3
The problem is not reproducible for me. If I do exactly as you stated I do get all inner HTML just fine. Which version of JSoup are you using? My check was done with version 1.8.3luksch
I'm using also 1.8.3 - last version and it dosn't work... You get the result the entire div content ?Paul

3 Answers

2
votes

Try this:

Elements el = doc.select("div.entry-body");

instead of this:

Element el = doc.select("div.entry-body").first();

and then:

for(Element e : el){
    e.html();
}

EDIT

Maybe you'll get you result if you do that this way: I have try to do it and it give a correct result. Elements el = doc.select("a.asset-img-link");

1
votes

As mentioned in the comments to the OP, I don't get it. Here is my reproduction of the problem, and it does exactly what you want:

String html = ""
    +"<div class=\"entry-content\">"
    +"   <div class=\"entry-body\">"
    +"      <p><strong>Text 1</strong></p>"
    +"      <p><strong> <a class=\"asset-img-link\" href=\"http://example.com\" style=\"display: inline;\"><img alt=\"IMG_7519\" class=\"asset  asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive\" src=\"http://example.com\" style=\"width: 500px;\" title=\"IMG_7519\" /></a><br /></strong></p>"
    +"      <p><em>Text 2</em> </p>"
    +"   </div>"
    +"</div>"
    ;
Document doc = Jsoup.parse(html);
Element el = doc.select("div.entry-body").first();
System.out.println(el.html());

This results in the following output:

<p><strong>Text 1</strong></p> 
<p><strong> <a class="asset-img-link" href="http://example.com" style="display: inline;"><img alt="IMG_7519" class="asset  asset-image at-xid-6a00d8341c648253ef01b7c8114e72970b img-responsive" src="http://example.com" style="width: 500px;" title="IMG_7519"></a><br></strong></p> 
<p><em>Text 2</em> </p>
1
votes

In your case, you'd use

  doc.select("div[name=entry-body]") to select that specific <div>

acording to this cookbook