0

I have this xml structure:

<markers>
<marker provincia="Guayas" lat="-2.33" lng="-77.63" />
<marker provincia="Pichincha" lat="-3.23" lng="-81.13" />
</markers>

and I want to show it in a listview. I have based in this example but this have other xml structure. I think I need to put the parameter getAttributeCount(), I found this sample but I don´t understand very well. Can you guide me?. This is the code modify:

package com.makemyandroidapp.example.stacksites;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;

public class SitesXmlPullParser {

    static String KEY_MARKER = "marker";

    public static List<StackSite> getStackSitesFromFile(Context ctx) {
        List<StackSite> stackSites;
        stackSites = new ArrayList<StackSite>();

        try {
            // Get our factory and PullParser
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();

            // Open up InputStream and Reader of our file.
            FileInputStream fis = ctx.openFileInput("StackSites.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

            xpp.setInput(reader);
            int eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                String tagname = xpp.getName();

                switch (eventType) {
                case XmlPullParser.START_TAG:

                    break;

                case XmlPullParser.END_TAG:
                    if (tagname.equals(KEY_MARKER)) {
                        KEY_MARKER = xpp.getAttributeValue(null, "provincia");
                        KEY_MARKER = xpp.getAttributeValue(null, "lat");
                        KEY_MARKER = xpp.getAttributeValue(null, "lng");    
                    } 
                        else{
                    } 
                    break;

                default:
                    break;
                }
                //move on to next iteration
                eventType = xpp.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // return the populated list.
        return stackSites;
    }
}
2

1 Answer 1

1

I have found the solution for this:

public class SitesXmlPullParser {
    static final String KEY_MARKERS = "markers";
    static final String KEY_MARKER= "marker";

    public static List<StackSite> getStackSitesFromFile(Context ctx) {

        // List of StackSites that we will return
        List<StackSite> stackSites;
        stackSites = new ArrayList<StackSite>();

        // temp holder for current StackSite while parsing
        StackSite curStackSite = null;          

        try {
            // Get our factory and PullParser
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();

            // Open up InputStream and Reader of our file.
            FileInputStream fis = ctx.openFileInput("StackSites.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

            // point the parser to our file.
            xpp.setInput(reader);

            // get initial eventType
            int eventType = xpp.getEventType();

            // Loop through pull events until we reach END_DOCUMENT
            while (eventType != XmlPullParser.END_DOCUMENT) {
                // Get the current tag
                String tagname = xpp.getName();

                // React to different event types appropriately
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase(KEY_MARKER)) {
                        // If we are starting a new <markers> block we need
                        //a new StackSite object to represent it
                        curStackSite = new StackSite();
                    }
                    break;

                case XmlPullParser.END_TAG:                 
                    if (tagname.equals(KEY_MARKER)) {                                                       
                        String title = xpp.getAttributeValue(null, "provincia");                
                        String lat = xpp.getAttributeValue(null, "lat");
                        String lng = xpp.getAttributeValue(null, "lng");                        
                        curStackSite.setTitulo(title);                      
                        curStackSite.setLat(lat);
                        curStackSite.setLng(lng);                       
                        stackSites.add(curStackSite);                                   
                    }                   
                    break;
                default:
                    break;              }
                //move on to next iteration
                eventType = xpp.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // return the populated list.
        return stackSites;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.