Parsing XML wont display all items.

Posted by Nauman A on Stack Overflow See other posts from Stack Overflow or by Nauman A
Published on 2011-01-17T15:48:45Z Indexed on 2011/01/17 15:53 UTC
Read the original article Hit count: 252

Filed under:
|
|
|

I have this code but the toast wont display any message what is wrong with my code.. I can get the value from link, linknext but title wont bring out any value. ( I am not very bright with writing code so please suggest anything you may feel like.

        final Button button = (Button) findViewById(R.id.Button01);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click


            try {

                URL url = new URL(
                        "http://somelink.com=" + Link.setFirst_link);
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new InputSource(url.openStream()));
                doc.getDocumentElement().normalize();

                NodeList nodeList = doc.getElementsByTagName("item");

                /** Assign textview array lenght by arraylist size */



                for (int i = 0; i < nodeList.getLength(); i++) {

                    Node node = nodeList.item(i);


                    Element fstElmnt = (Element) node;
                    NodeList nameList = fstElmnt.getElementsByTagName("link");
                    Element nameElement = (Element) nameList.item(0);
                    nameList = nameElement.getChildNodes();
                    String img = (((Node) nameList.item(0)).getNodeValue());

                    NodeList websiteList = fstElmnt.getElementsByTagName("linknext");
                    Element websiteElement = (Element) websiteList.item(0);
                    websiteList = websiteElement.getChildNodes();
                    String nextlink = (((Node) websiteList.item(0)).getNodeValue());
                    Link.setFirst_link = nextlink;
                    Drawable drawable = LoadImageFromWebOperations(img);
                    imgView.setImageDrawable(drawable);

                    NodeList titleList = fstElmnt.getElementsByTagName("title");
                    Element titleElement = (Element) titleList.item(0);
                    websiteList = titleElement.getChildNodes();
                    String title = (((Node) titleList.item(0)).getNodeValue());

                    Context context = getApplicationContext();
                    CharSequence text = title;
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();

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



        }
    });


    /** Set the layout view to display 

*/ }

Here is the xml file

<?xml version="1.0"?> 
<maintag> 
  <item> 
    <link>http://image.com/357769.jpg?40</link> 
    <linknext>http://www.image.com</linknext> 
    <title>imagename</title> 
  </item> 
</maintag> 

© Stack Overflow or respective owner

Related posts about java

Related posts about android