-2

What I need is to get first image of string... I have

notice.getFullContent()

which returns html string...p tags...strong tags...img tag...

What I need is to get the src of the first image. I'm doing this:

Document doc = Jsoup.parse(notice.getFullContent());
Element imageElement = doc.select("img").first();
imageURL = imageElement.absUrl("src");

But when i go to page, it doesn't work. I'm in jsp of liferay instance. Thanks in advance,

SOLVED

Document doc = Jsoup.parse(notice.getFullContent());
String imageURL = "";
String description = "";
Element imageElement = doc.select("img").first();
if(imageElement != null)
{
    imageURL = imageElement.attr("src");
}
Element descriptionElement = doc.select("p").first();
if(descriptionElement != null)
{
    description = descriptionElement.text();
}
if(description.isEmpty() || description == null)
{
    description = notice.getTitle();
}

The purpose was use these values to add meta og tags, like this:

<liferay-util:html-top>
       <meta content="<%="WEBSITE/post?id="+id %>" property="og:url">
       <meta content="article" property="og:type">
       <meta content="<%=notice.getTitle() %>" property="og:title">
       <meta content="<%=imageURL %>" property="og:image">
       <meta content="<%=description %>" property="og:description">
</liferay-util:html-top>

thanks!

Daniel Garcia Sanchez
  • 2,306
  • 5
  • 21
  • 35

1 Answers1

-1

page is not visible...and website returns "page not available..."...I think i need to import Jsoup...but dont know well the line to add...

If you don't have the Jsoup library, that code wont work. To add the Jsoup library manually, you have to download the .jar file for it and then add it to your lib folder (inside WEB-INF).

Also your code here:

Document doc = Jsoup.parse(notice.getFullContent());
Element imageElement = doc.select("img").first();
imageURL = imageElement.absUrl("src");

Is not enough code to understand what your problem is. Variables are missing in that example. Check out this link to understand what you should post

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44