/**
* Created by John on 2016/3/29.
*/
public class XmlParser {
private static final String ns = null; /**
* 创建一个解析器
* @param is 传入数据的输入流
* @return 返回一个装有entry标签的arraylist
* @throws XmlPullParserException 抛出异常
* @throws IOException 抛出异常
*/
public List parse(InputStream is) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(is, null);
parser.nextTag();
return readFeed(parser);
} finally {
is.close();
}
} /**
* 获取一个feed标签
* @param parser 传入解析器
* @return 返回一个List,将所有的entry字段封装在里面
* @throws IOException 抛出异常
* @throws XmlPullParserException 抛出异常
*/
private List readFeed(XmlPullParser parser) throws IOException, XmlPullParserException {
List entries = new ArrayList();
parser.require(XmlPullParser.START_TAG,ns,"feed");
while(parser.next() != XmlPullParser.END_TAG){
continue;
}
String name = parser.getName();
if (name.equals("entry")){
entries.add(readEntry(parser));
}else {
skip(parser);
}
return entries;
} /**
* 获取entry标签
* @param parser 传入解析器
* @return 返回一个Entry数据,传入实体类以便于使用
* @throws IOException 抛出异常
* @throws XmlPullParserException 抛出异常
*/
private Entry readEntry(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG,ns,"entry");
String title = null;
String link = null;
String summary = null;
while (parser.next() != XmlPullParser.END_TAG){
if (parser.getEventType() != XmlPullParser.START_TAG){
continue;
}
String name = parser.getName();
if (name.equals("title")){
title = readTitle(parser);
}else if (name.equals("link")){
link = readLink(parser);
}else if(name.equals("summary")){
summary = readSummary(parser);
}else {
skip(parser);
}
}
return new Entry(title,summary,link);
} /**
* 获取summary标签
* @param parser 传入解析器
* @return 返回一个标签
* @throws IOException 抛出异常
* @throws XmlPullParserException 抛出异常
*/
private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG,ns,"summary");
String summary = readText(parser);
parser.require(XmlPullParser.END_TAG,ns,"summary");
return summary;
} /**
* 获取link标签
* @param parser 传入解析器
* @return 返回一个link标签
* @throws IOException 抛出异常
* @throws XmlPullParserException 抛出异常
*/
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
String link = "";
parser.require(XmlPullParser.START_TAG,ns,"link");
String tag = parser.getName();
String relType = parser.getAttributeValue(null,"rel");
if (tag.equals("link")){
if (relType.equals("alternate")){
link = parser.getAttributeValue(null,"href");
parser.nextTag();
}
}
parser.require(XmlPullParser.END_TAG,ns,"link");
return link;
} /**
* 获取title标签
* @param parser 传入解析器
* @return 返回一个标签
* @throws IOException 抛出异常
* @throws XmlPullParserException 抛出异常
*/
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG,ns,"title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG,ns,"title");
return title;
} /**
* 获取标签中的文本
* @param parser 传入解析器
* @return 返回一个字符串
* @throws IOException 抛出异常
* @throws XmlPullParserException 抛出异常
*/
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT){
result = parser.getText();
parser.nextTag();
}
return result;
} /**
* 跳过不需要的标签
* @param parser 传入解析工具parser
* @throws XmlPullParserException 抛出异常
* @throws IOException 抛出异常
*/
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG){
// throw new IllegalStateException();
}
int depth = 1;
while (depth != 0){
switch (parser.next()){
case XmlPullParser.END_TAG:
depth --;
break;
case XmlPullParser.START_TAG:
depth ++;
break;
}
}
} /**
* 创建一个Entry实体类
*/
public static class Entry{
public final String title;
public final String link;
public final String summary; private Entry(String title, String summary, String link) {
this.title = title;
this.summary = summary;
this.link = link;
}
}
}
这是一个简单的工具类,如果需要获取其他字段也可以自己添加~!