hi folks, i've been struggling to find a way to read in a url value from within a tag in an xml feed. im so close but i just can't seem to crack it.
i started out using this http://www.devx.com/wireless/Article/39810/1954 as a guide which works great but customising it to read in an attribute of an element is causing me problems. the feed is here: http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml within the xml's 'item' element is <media:thumbnail width="X" height="X" url="X"/> it's the url attribute im after. i can get the title back fine but the structure of the media:thumbnail tag means im missing something! what's interesting(!?) is the fact that when i remove the loop and only go for the first item, i get the image back! the line with strImg = imgElement.getAttribute("url") just won't work within the loop... any ideas are greatly appreciated. this is very rough n ready, im learning :-) ======================== package apps.httpDownload; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; public class httpDownload extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<String> dataReturned = DownloadRSS("http:// newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml"); TextView txt = (TextView) findViewById(R.id.text); for (int i = 0; i < dataReturned.size(); i++) { if (i < 10) txt.setText(txt.getText() + "\n" + dataReturned.get(i) + "\n"); Bitmap bitmap = DownloadImage(dataReturned.get(1)); ImageView img = (ImageView) findViewById(R.id.img); img.setImageBitmap(bitmap); } } private InputStream OpenHttpConnection(String urlString) throws IOException { InputStream in = null; int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); try{ HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception ex) { throw new IOException("Error connecting"); } return in; } private Bitmap DownloadImage(String URL) { Bitmap bitmap = null; InputStream in = null; try { in = OpenHttpConnection(URL); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e1) { e1.printStackTrace(); } return bitmap; } private ArrayList<String> DownloadRSS(String URL) { ArrayList<String> dataArray = new ArrayList<String>(); InputStream in = null; try { in = OpenHttpConnection(URL); Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); doc = db.parse(in); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } doc.getDocumentElement().normalize(); NodeList itemNodes = doc.getElementsByTagName("item"); String strTitle = ""; String strImg = ""; for (int i = 0; i < itemNodes.getLength(); i++) { Node itemNode = itemNodes.item(i); if (itemNode.getNodeType() == Node.ELEMENT_NODE) { Element itemElement = (Element) itemNode; NodeList titleNodes = itemElement.getElementsByTagName("title"); Element titleElement = (Element) titleNodes.item (0); NodeList imgNodes = itemElement.getElementsByTagName("media:thumbnail"); Element imgElement = (Element) imgNodes.item(0); NodeList textNodes = titleElement.getChildNodes(); strTitle = ((Node) textNodes.item(0)).getNodeValue (); //strImg = imgElement.getAttribute("url"); dataArray.add(strTitle); dataArray.add(strImg); } } } catch (IOException e1) { e1.printStackTrace(); } return dataArray; } } ======================== --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---

