2011-08-09 4 views
6

주어진 문자열에서 HTML 이미지 태그 URL을 가져 오려고합니다. 그것을 얻으려면 정규식이 있어야합니다. 그러나 그것을하는 방법을 모른다. 아무도 이것에 나를 도울 수 있습니까.android java 문자열에서 html image 태그를 가져옵니다.

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 ? 

는 난 단지 http://xyz.com/par.jpg 문자열

답변

7

에서 참조 할 수 this 질문을 참조하십시오 할 수 있습니다. 기본적으로는 사용 말한다 :

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

는 아주 쉽게이 작업을 수행 할 수 있습니다. 비록 사소한 줄이긴하지만 과도 할 수 있습니다.

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

나는 jsoup을 사용합니다. 가볍고 사용하기 쉽습니다. 일부 버전은 Java 1.5와 호환되지 않지만 문제를 해결 한 것으로 보입니다. 모든 가져 오기 jsoap의

String html = str; 
Document doc = Jsoup.parse(html); 
Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png 
1

프리스트 :

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

그런 다음 당신은이를 사용할 수 있습니다

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; 
} 
관련 문제