public class XMLParser {
/**
* 解析服务器的配置文件
* @param is 从服务器获得配置的文件的流
*/
public static UpdateInfo parser(InputStream is){
UpdateInfo info=null;
try {
//获得pull解析实例
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
XmlPullParser xmlParser=factory.newPullParser();
//初始化解析器
xmlParser.setInput(is, "UTF-8");
int eventType=xmlParser.getEventType();
//文档没有解析完
while(eventType!=XmlPullParser.END_DOCUMENT){
switch (eventType) {
case XmlPullParser.START_TAG:
if(xmlParser.getName().equals("info")){
info=new UpdateInfo();
}else if(xmlParser.getName().equals("version")){
info.setVersion(xmlParser.nextText());
}else if(xmlParser.getName().equals("desc")){
info.setDesc(xmlParser.nextText());
}else if(xmlParser.getName().equals("apkurl")){
info.setApkUrl(xmlParser.nextText());
}
break;
}
eventType=xmlParser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return info;
}
}
对应的update.xml
<?xml version="1.0" encoding="utf-8"?>
<info>
<version>2.0</version>
<description>xxxxxxxxxx</description>
<apkurl>http://192.168.253.1:8080/xxxxxxx.apk</apkurl>
</info>