简体   繁体   中英

I am trying to parse XML from URL but it just shows me only first 3 elements not all

I am trying to parse this url http://rssmix.com/u/8304287/rss.xml

I have tried this code which i got from a website it was working fine with their own link but when i try it on my link it just shows me first 3 elements results. I want to parse whole XML but i don't know where the mistake is may be in for loop or something please help. Thank you.

public class MainActivity extends AppCompatActivity {




    // Declare variables
    TextView textview;
    NodeList nodelist;
    ProgressDialog pDialog;
    // Insert image URL
    String URL = "http://rssmix.com/u/8304287/rss.xml";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Locate a TextView in your activity_main.xml layout
        textview = (TextView) findViewById(R.id.text);
        // Execute DownloadXML AsyncTask
        new DownloadXML().execute(URL);
    }

    // DownloadXML AsyncTask
    private class DownloadXML extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressbar
            pDialog = new ProgressDialog(MainActivity.this);
            // Set progressbar title
            pDialog.setTitle("Android Simple XML Parsing using DOM Tutorial");
            // Set progressbar message
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            // Show progressbar
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... Url) {
            try {
                URL url = new URL(Url[0]);
                DocumentBuilderFactory dbf = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                // Download the XML file
                Document doc = db.parse(new InputSource(url.openStream()));
             //   doc.getDocumentElement();
                // Locate the Tag Name
                nodelist = doc.getElementsByTagName("channel");
                Log.v("BIGASS",doc.toString());




            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(Void args) {

            for (int temp = 0; temp < nodelist.getLength(); temp++) {
                Node nNode = nodelist.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    // Set the texts into TextViews from item nodes
                    // Get the title
                    textview.setText(textview.getText() + "Title : "
                            + getNode("title", eElement) + "\n" + "\n");
                    // Get the description
                    textview.setText(textview.getText() + "Description : "
                            + getNode("description", eElement) + "\n" + "\n");
                    // Get the link
                    textview.setText(textview.getText() + "Link : "
                            + getNode("link", eElement) + "\n" + "\n");
                    // Get the date

                }
            }
            // Close progressbar
            pDialog.dismiss();
        }
    }

    // getNode function
    private static String getNode(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
                .getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    }
}

In you xml there is only one channel tag as a result when you execute

nodelist = doc.getElementsByTagName("channel");

you will get only one element. My understanding is that you want a list of all items, for that you should do something like

items = channelNode.getElementsByTagName("item");

in your for loop. your doPost method will look something like this

@Override
        protected void onPostExecute(Void args) {

            for (int temp = 0; temp < nodelist.getLength(); temp++) {
                Node nNode = nodelist.item(temp);// This will be your channel node

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
//here call 
List<Elements> items=eElement.getElementsByTagName("item") //This is give list of all items
                    //loop over the items and parse title,description and link.

                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM