我在使用 Web 服务时遇到问题。我想使用 解析数据simple xml parser
。如果有人使用过这个简单的 xml 解析器,请帮忙。提前致谢...:)
问问题
2735 次
2 回答
0
创建一个解析器并从主活动发送您的 URL。
public class GoogleXmlParser {
private static final String ns=null;
public List<Entry>parse(InputStream in) throws XmlPullParserException,IOException
{
try {
XmlPullParser parser=Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in,null);
parser.nextTag();
return reedFeed(parser);
}finally{
in.close();
}
}
private List<Entry>reedFeed(XmlPullParser parser) throws XmlPullParserException,IOException
{
List<Entry> entries = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "rss");
//String name1=parser.getName();
System.out.println(parser.getName());
//String tag=;
while (parser.nextTag()!=XmlPullParser.END_TAG) {
if(parser.getEventType()!=XmlPullParser.START_TAG){
continue;
}
String name=parser.getName();
if(name.equals("channel")){
String tag=parser.getName();
System.out.println("tag"+tag);
}
else if(name.equals("item")){
System.out.println("tag"+parser.getName());
entries.add(readEntry(parser));
}else{
skip(parser);
}
}
return entries;
}
public static class Entry
{
public String title=null;
public String link=null;
public String description=null;
private Entry(String title, String description, String link) {
this.title = title;
this.description = description;
this.link = link;
}
}
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException,IOException
{
parser.require(XmlPullParser.START_TAG, ns, "item");
System.out.println(parser.getDepth());
String title = null;
String description = null;
String link = 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("description")){
description = readSummary(parser);
}else{
skip(parser);
}
}
return new Entry(title, description, link);
}
private String readTitle(XmlPullParser parser)throws IOException,XmlPullParserException
{
//parser.require(XmlPullParser.START_TAG, ns, "item");
//parser.next();
parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}
private String readLink(XmlPullParser parser)throws IOException,XmlPullParserException
{
String link="";
parser.require(XmlPullParser.START_TAG, ns, "link");
link=parser.nextText();
parser.require(XmlPullParser.END_TAG, ns, "link");
return link;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "description");
String summary = readText(parser);
//System.out.println(summary);
parser.require(XmlPullParser.END_TAG, ns, "description");
return summary;
}
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;
}
}
}
}
于 2013-10-31T04:38:04.620 回答
0
由于 API 非常低级,因此直接使用 pull parser 容易出错且困难。请尝试https://gitlab.com/mvysny/konsume-xml
于 2019-04-13T04:59:48.173 回答