如何在本地XML文件中使用Android中的SAXparser解析“img src”标记

时间:2012-10-30 07:45:24

标签: java android xml parsing

<description>
div class=&quot;field field-type-filefield field-field-book-review-image&quot;&gt;
    &lt;div class=&quot;field-items&quot;&gt;
            &lt;div class=&quot;field-item odd&quot;&gt;
                    &lt;img  class=&quot;imagefield imagefield-field_book_review_image&quot; width=&quot;500&quot; height=&quot;741&quot; alt=&quot;&quot; src=&quot;**http://sampada.net/files/good%20earth.JPG?1327387980**&quot; /&gt;        &

</description>

3 个答案:

答案 0 :(得分:1)

你最好使用DOM而不是SAX。只需遍历名为“img”的所有元素,获取“src”属性,构建URL并激活AsyncTask以下载流。

答案 1 :(得分:0)

尝试以下开源库:

http://htmlcleaner.sourceforge.net/

以下是一些HTML解析器:

http://java-source.net/open-source/html-parsers

答案 2 :(得分:0)

如果您只想要1张照片,我会用它:

static final String image_URL = "http://sampada.net/files/good%20earth.JPG?1327387980";

//in the oncreate

ImageView bmImage = (ImageView)findViewById(R.id.<<your image id from the xml>>);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage(image_URL, bmOptions);
bmImage.setImageBitmap(bm);
//under the oncreate

private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{       
    Bitmap bitmap = null;
    InputStream in = null;       
    try {
       in = OpenHttpConnection(URL);
       bitmap = BitmapFactory.decodeStream(in, null, options);
       in.close();
    } catch (IOException e1) {
    }
    return bitmap;               
}

private InputStream OpenHttpConnection(String strURL) throws IOException{
    InputStream inputStream = null;
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();

    try{
        HttpURLConnection httpConn = (HttpURLConnection)conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    }
    catch (Exception ex)
    {
    }
    return inputStream;
}

希望这会对你有所帮助。