1. Check out our app, Forums for Android! Download and leave feedback here!

Reading data from XML document

Discussion in 'Application Development' started by kaibuki, Apr 12, 2010.

  1. kaibuki

    kaibuki New Member
    Thread Starter
    5

    Apr 8, 2010
    4
    0
    5
    Hi Guys,

    This is my first post on this forum, :)

    I am trying to read a XML document from internet and view all the data from it in my app.
    unfortunately so far I can only read one node.
    any one can help me with the code/suggestions on how to read XML data and display on screen + can later use that data.

    Thanks alot..looking forward for replies.

    cheers..

    Kai!
     

    Advertisement

  2. tommoor

    tommoor Member
    36

    Apr 11, 2010
    23
    1
    36
  3. kaibuki

    kaibuki New Member
    Thread Starter
    5

    Apr 8, 2010
    4
    0
    5
    Thanx tommoor!

    but I am looking for a compelete sample code.
    as the sample code available online, is only able to read one node, I want to read all the node.

    Looking forward for replies..

    Thanks!!
     
  4. malar

    malar New Member
    5

    Dec 29, 2010
    1
    0
    5
    try this code.
    ArrayList gridImgId = new ArrayList();
    ArrayList gridImgName = new ArrayList();
    ArrayList gridImgThump = new ArrayList();
    ArrayList gridUrl = new ArrayList();
    try
    {
    URL url = new URL("http://thebeswallpapers.com/api/androidxml.php?action=getCategoryList");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));
    doc.getDocumentElement().normalize();
    NodeList nodeList = doc.getElementsByTagName("category");
    for(int a = 0; a < nodeList.getLength(); a++)
    {
    //nodeList.item(0).getChildNodes();
    NodeList child=nodeList.item(a).getChildNodes();
    String imgid=child.item(0).getFirstChild().getNodeValue();
    String imgname=child.item(1).getFirstChild().getNodeValue();
    String imgthump=child.item(2).getFirstChild().getNodeValue();
    gridUrl.add("http://thebesthdwallpapers.com/wallpapers/"+imgid+"/"+imgthump);
    gridImgId.add(imgid);
    gridImgName.add(imgname);
    gridImgThump.add(imgthump);
    }
    /*for(int a = 0; a < 10; a++)
    {
    gridUrl.add("http://www.xyz.com/samples/images/images"+ a +".png");

    }*/
    }
    catch(Exception e)
    {
    System.out.println("XML Pasing Excpetion = " + e);
    }


    String[] urls = (String[]) gridUrl.toArray(new String[gridUrl.size()]);
    String[] ImageId = (String[]) gridImgId.toArray(new String[gridImgId.size()]);
    String[] ImageName = (String[]) gridImgName.toArray(new String[gridImgName.size()]);
    String[] ImageThump = (String[]) gridImgThump.toArray(new String[gridImgThump.size()]);
     

Share This Page

Loading...