Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android java get html image tag from string

Im trying to get HTML image tag url from the given string. There should be some regular expression to get it. But don't know how to do it. Can anyone help me on this.

e.g.

I have string like this with <br> some HTML<b>tag</b>
with <img src="http://xyz.com/par.jpg" align="left"/> image tags in it.
how can get it ?

I want only http://xyz.com/par.jpg from the string

like image 965
Umakant Patil Avatar asked Aug 09 '11 14:08

Umakant Patil


4 Answers

Please see this question for reference. Basically it says to use:

String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
like image 132
Jack Avatar answered Oct 15 '22 02:10

Jack


I use jsoup. It is pretty easy to use and lightweight. Some versions were not Java 1.5 compatible but it appears they fixed the issue.

String html = str;
Document doc = Jsoup.parse(html);
Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png
like image 36
Frohnzie Avatar answered Oct 15 '22 03:10

Frohnzie


Frist of All Import jsoap:

compile group: 'org.jsoup', name: 'jsoup', version: '1.7.2'

Then you can Use this:

private ArrayList pullLinks(String html) {
    ArrayList links = new ArrayList();
    Elements srcs = Jsoup.parse(html).select("[src]"); //get All tags containing "src"
    for (int i = 0; i < srcs.size(); i++) {
        links.add(srcs.get(i).attr("abs:src")); // get links of selected tags
    }
    return links;
}
like image 29
Mahdi Astanei Avatar answered Oct 15 '22 01:10

Mahdi Astanei


An XMLPullParser can do this pretty easily. Although, if it is a trivially small string, it may be overkill.

     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     XmlPullParser xpp = factory.newPullParser();

     xpp.setInput( new StringReader ( "<html>I have string like this with <br> some HTML<b>tag</b> with <img src=\"http://xyz.com/par.jpg\" align=\"left\"/> image tags in it. how can get it ?</html>" ) );
     int eventType = xpp.getEventType();
     while (eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_TAG && "img".equals(xpp.getName()) {
          //found an image start tag, extract the attribute 'src' from here...
      }
      eventType = xpp.next();
     }
like image 33
nicholas.hauschild Avatar answered Oct 15 '22 01:10

nicholas.hauschild