我有这个xml结构:
<markers>
<marker provincia="Guayas" lat="-2.33" lng="-77.63" />
<marker provincia="Pichincha" lat="-3.23" lng="-81.13" />
</markers>
我希望在列表视图中显示它。我有这个example,但这有其他xml结构。我想我需要把参数getAttributeCount(),我发现这个sample但我不太了解。你能指导我吗?这是代码修改:
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;
}
}
答案 0 :(得分:0)
我找到了解决方法:
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;
}
}