Computer Gaga
Computer Gaga

Reputation: 161

How to parse "img src" tag in using SAXparser in android from a Local XML file

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

Upvotes: 1

Views: 966

Answers (3)

Raffaele
Raffaele

Reputation: 20885

You'd better use DOM than SAX. Simply iterate through all elements named "img" , get the "src" attribute, build a URL and fire an AsyncTask to download the stream.

Upvotes: 1

Pieter Tielemans
Pieter Tielemans

Reputation: 624

if you only want 1 pic i would use this:

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

hope this helps you.

Upvotes: 0

Praful Bhatnagar
Praful Bhatnagar

Reputation: 7435

Try the following open source library:

http://htmlcleaner.sourceforge.net/

Following are some more HTML parser:

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

Upvotes: 0

Related Questions