开发者

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


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

String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";


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


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;
}


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();
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜