解析XML不会显示所有项目

时间:2011-01-17 15:48:45

标签: java android xml parsing

我有这个代码,但吐司不会显示任何消息我的代码有什么问题..我可以从链接获取值,linknext但标题不会带来任何价值。 (编写代码时我不是很聪明,所以请提出你想要的任何建议。

        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 

* /     }

这是xml文件

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

1 个答案:

答案 0 :(得分:1)

Element titleElement = (Element) titleList.item(0);
titleList = titleElement.getChildNodes();            // <<< here you wrote websiteList instead (copy & paste error I think ;)