XmlPullParser xmlPullParser = Xml.newPullParser();
Product p = null;
try {
AssetManager manager = getAssets();
//获取assets资源的管理者,assets资源位于Main目录下,没有的话自己创建,new Folder- AssetsFolder
InputStream in = manager.open("result.xml");//解析的源
xmlPullParser.setInput(in,"gbk");//解析的格式
int eventType = xmlPullParser.getEventType();//获得一个事件的类型
while(eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_TAG)
{
if("product".equals(xmlPullParser.getName()))
{
p = new Product();
String type = xmlPullParser.getAttributeValue(0);
//<product type="mobile"> 获取的是mobile值,如果要获取type名,则使用getAttributeName
// 参数0表示第一个索引位置,一个括号中可能有多个索引
p.setType(type);
System.out.println(type);
}
else if("phonenum".equals(xmlPullParser.getName()))
{
String phonenum = xmlPullParser.nextText();
p.setPhonenum(phonenum);
}
else if("location".equals(xmlPullParser.getName()))
{
String location = xmlPullParser.nextText();
p.setLocation(location);
}
else if("phoneJx".equals(xmlPullParser.getName()))
{
String phoneJx = xmlPullParser.nextText();
p.setPhoneJx(phoneJx);
}
}
eventType = xmlPullParser.next();//手动 挪动 "指针 "
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
解析XML:xmlPullParser
最新推荐文章于 2021-05-27 23:51:22 发布
