本帖最后由 冯盼 于 2012-12-4 23:21 编辑
主要的读取XML程序:
public class pullXML {
public static List<person> readXML(InputStream instream)throws Exception
{
List<person> persons = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(instream, "UTF-8");
int eventcode = parser.getEventType();
person pers=null;
while(eventcode!=XmlPullParser.END_DOCUMENT)
{
switch(eventcode)
{
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(parser.getName()))
{
pers = new person();
pers.setid(Integer.valueOf(parser.getAttributeValue(null, "id")));
}
else if(pers!=null)
{
if("name".equals(parser.getName()))
{
pers.setname(parser.nextText());
}
else if("age".equals(parser.getName()))
{
pers.setage(new Short(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName()))
{
persons.add(pers);
}
break;
}
eventcode = parser.next();
}
return persons;
}
}
|
|