Paul February 2016

Jsoup - extract html from element

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>

Answers


micsha123 February 2016

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");


Stanojkovic February 2016

In your case, you'd use

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

acording to this cookbook


luksch February 2016

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>

Post Status

Asked in February 2016
Viewed 2,709 times
Voted 6
Answered 3 times

Search




Leave an answer