今天做项目,与后台交互,发现返回的是xml格式的文本,如下:
<Result><Status><![CDATA[failure]]></Status><Message><![CDATA[The username you entered is incorrect.]]></Message></Result>
那么如何解析这样的格式呢???
因为Android官方推荐开发者们使用Pull解析技术,所以就试试它吧!
先随便照着写一个,看看这种pull解析方式的流程:
/**
*
* @file AndroidPullXML.java
* @author Samuel.Cai
* @date Nov 29, 2011
*/
public class AndroidPullXML {
public static void readXML(String resultStr) throws Exception{
readXML(resultStr,"utf-8");
}
public static void readXML(String resultStr,String inputEncoding) throws Exception{
InputStream inputStream = new ByteArrayInputStream(resultStr.getBytes());
readXML(inputStream,inputEncoding);
}
public static void readXML(InputStream inputStream,String inputEncoding) throws Exception{
XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = pullParserFactory.newPullParser();
pullParser.setInput(inputStream, inputEncoding);
int eventType = pullParser.getEventType();
LoginResult loginResult = null;
while(eventType != XmlPullParser.END_DOCUMENT){
String nodeName = pullParser.getName();
System.out.println("SA: nodeName = " + nodeName);
eventType = pullParser.next();
}
}
}
结果,logcat打印如下:
11-29 16:16:55.311: I/System.out(588): SA: nodeName = null
11-29 16:16:55.311: I/System.out(588): SA: nodeName = Result
11-29 16:16:55.311: I/System.out(588): SA: nodeName = Status
11-29 16:16:55.311: I/System.out(588): SA: nodeName = null
11-29 16:16:55.311: I/System.out(588): SA: nodeName = Status
11-29 16:16:55.311: I/System.out(588): SA: nodeName = Message
11-29 16:16:55.311: I/System.out(588): SA: nodeName = null
11-29 16:16:55.321: I/System.out(588): SA: nodeName = Message
11-29 16:16:55.321: I/System.out(588): SA: nodeName = Result
发现了问题:
像 <![CDATA[failure]]>这样的东西它直接忽略,认为是null
注:CDATA只表示中间是一段完整的片段,不受一些特殊符号的影响,没别的作用,直接获取Status的值应该就可以了,但是应该如何去取呢?
查阅资料:http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
进行修改实验:
public class AndroidPullXML {
public static void readXML(String resultStr) throws Exception{
readXML(resultStr,"utf-8");
}
public static void readXML(String resultStr,String inputEncoding) throws Exception{
InputStream inputStream = new ByteArrayInputStream(resultStr.getBytes());
readXML(inputStream,inputEncoding);
}
public static void readXML(InputStream inputStream,String inputEncoding) throws Exception{
XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = pullParserFactory.newPullParser();
pullParser.setInput(inputStream, inputEncoding);
int eventType = pullParser.getEventType();
LoginResult loginResult = null;
while(eventType != XmlPullParser.END_DOCUMENT){
String nodeName = pullParser.getName();
System.out.println("SA: nodeName = " + nodeName);
// new add..
if(eventType == XmlPullParser.TEXT){
System.out.println("SA: text = " + pullParser.getText());
}
eventType = pullParser.next();
}
}
}
输出结果:------
11-29 16:40:17.792: I/System.out(650): SA: nodeName = null
11-29 16:40:17.792: I/System.out(650): SA: nodeName = Result
11-29 16:40:17.792: I/System.out(650): SA: nodeName = Status
11-29 16:40:17.792: I/System.out(650): SA: nodeName = null
11-29 16:40:17.792: I/System.out(650): SA: text = failure
11-29 16:40:17.801: I/System.out(650): SA: nodeName = Status
11-29 16:40:17.801: I/System.out(650): SA: nodeName = Message
11-29 16:40:17.801: I/System.out(650): SA: nodeName = null
11-29 16:40:17.801: I/System.out(650): SA: text = The username you entered is incorrect.
11-29 16:40:17.801: I/System.out(650): SA: nodeName = Message
11-29 16:40:17.801: I/System.out(650): SA: nodeName = Result
总结:pull解析方式,它的eventType有以下几种:
START_DOCUMENT
START_TAG
TEXT
END_TAG
END_DOCUMENT
对于类型为 XmlPullParser.TEXT的 EventType ,你调用 getName()方法只会返回null,因为它不存在什么 “节点名称”
现在开始实际编码:
参考资料:
查阅资料:http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
系列之Android XML解析技术(一)的帖子链接http://www.eoeandroid.com/thread-76224-1-1.html
系列之Android XML解析技术(二)的帖子链接http://www.eoeandroid.com/thread-90596-1-1.html
系列之Android XML解析技术(三)的帖子链接http://www.eoeandroid.com/thread-90600-1-1.html
系列之Android XML解析技术(四)的帖子链接http://www.eoeandroid.com/thread-91326-1-1.html
系列之Android XML解析技术(五)的帖子链接http://www.eoeandroid.com/thread-91332-1-1.html