Android pull 解析xml
本文主要把pull解析封装成一个方法,方便调用:
/**
* 解析xml
*
* @author TangRen
* @param content
* @return 下午2:46:27
*/
public static Map<String, String> decodeXml(String content) {
try {
Map<String, String> xml = new HashMap<String, String>();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader(content));
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
switch (event) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if ("xml".equals(nodeName) == false) {
xml.put(nodeName, parser.nextText());
}
break;
case XmlPullParser.END_TAG:
break;
}
event = parser.next();
}
return xml;
} catch (Exception e) {
e.printStackTrace();
Log.e("xml解析出现异常", e.toString());
}
return null;
}
调用:Map<String, String> xml = XMlUtil.decodeXml(arg1.get());
Log.d("code:",xml.get("code"));