1.Pull解析:
package com.zknu.xmlpull;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import com.zknu.entity.Facility;
import android.content.Context;
import android.util.Xml;
/**
* 作用:解析XML文档
*
* */
public class XmlPull {
/**
* 得到父元素
*
*
* */
static List<String> parents = null;
static String parent = null;
public static List<String> getParents(Context context, InputStream is)
throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
parents = new ArrayList<String>();
break;
case XmlPullParser.START_TAG:
String name = parser.getName();
if (name.equals("facilityType")) {
parent = parser.getAttributeValue(0);
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_TAG:
if (parent != null) {
parents.add(parent);
}
parent = null;
break;
default:
break;
}
eventType = parser.next();
}
return parents;
}
/**
* 得到子元素
*
*
*
* */
public static List<List<Facility>> getChildren(Context context, InputStream is)
throws Exception {
List<List<Facility>> children = new ArrayList<List<Facility>>();
List<Facility> child = null;
Facility facility = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
child = new ArrayList<Facility>();
break;
case XmlPullParser.START_TAG:
String name = parser.getName();
if (name.equals("facilityType")) {
child = new ArrayList<Facility>();
}
if (name.equals("facility")) {
facility = new Facility();
} else if (name.equals("id")) {
facility.setId(Integer.parseInt(parser.nextText()));
} else if (name.equals("name")) {
facility.setName(parser.nextText());
} else if (name.equals("state")) {
facility.setState(Integer.parseInt(parser.nextText()));
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_TAG:
String endname = pars