解析XML数据(一)
1pull解析方式
把String数据传入XmlPullParse.取得eventType=parse.getEventType(),判断
XmlPullParse.END_DOCUMENT,数据结尾(说明解析完成);
XmlPullParse.START_TAG,节点开头;
XmlPullParse.END_TAG,节点结尾.
取得parse.getName(),取得节点内元素标签名.
String xml;
XmlPullParseFactory factory=XmlPullFactory.newInstance();
XmlPullRarse parse=factory.newPullParse();
parse.setInput(new StringReader(xml));
int eventType=parse.getEventType();
String id=null;
String name=null;
String version=null;
while(eventType!=XmlParse.END_DOCUMENT){
String nodename=parse.getName();
Switch(eventType)){
case XmlPullParse.START_TAG:{
if("id".equals(nodename)){
id=parse.nextText();}
if("name".equals(nodename)){
name=parse.nextText();}
if("version".equals(nodename)){
version=parse.nextText();
}
break;
}
case XmlPullParse.END_TAG:{
if("app".equals(nodename)){
log.d("MainActivity","id is"+id);
log.d("MainActivity","name is"+name);
log.d("MainActivity","version is"+version);
}
break;
}
default:
break;
}
eventType=parse.getNext();
}
1pull解析方式
把String数据传入XmlPullParse.取得eventType=parse.getEventType(),判断
XmlPullParse.END_DOCUMENT,数据结尾(说明解析完成);
XmlPullParse.START_TAG,节点开头;
XmlPullParse.END_TAG,节点结尾.
取得parse.getName(),取得节点内元素标签名.
String xml;
XmlPullParseFactory factory=XmlPullFactory.newInstance();
XmlPullRarse parse=factory.newPullParse();
parse.setInput(new StringReader(xml));
int eventType=parse.getEventType();
String id=null;
String name=null;
String version=null;
while(eventType!=XmlParse.END_DOCUMENT){
String nodename=parse.getName();
Switch(eventType)){
case XmlPullParse.START_TAG:{
if("id".equals(nodename)){
id=parse.nextText();}
if("name".equals(nodename)){
name=parse.nextText();}
if("version".equals(nodename)){
version=parse.nextText();
}
break;
}
case XmlPullParse.END_TAG:{
if("app".equals(nodename)){
log.d("MainActivity","id is"+id);
log.d("MainActivity","name is"+name);
log.d("MainActivity","version is"+version);
}
break;
}
default:
break;
}
eventType=parse.getNext();
}