0
我正在使用enter link description here如何解析嵌套的RSS提要?
我能夠從URL獲取所有需要的數據。但我想獲取<item>...</item>
屬性作爲List。我可以逐個獲取所有屬性。我想要完整的item屬性列表。 他們的任何其他選項是否相同?
以下是rss解析的代碼。
public class HandleXML {
public volatile boolean parsingComplete = true;
private String title = "title";
private String link = "link";
private String description = "description";
private String image = "image";
private String imageTitle = "title";
private String imageDescription = "description";
private String imageLink = "link";
private String guid = "guid";
private String pubDate = "pubDate";
private String media_content = "media:content";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
private boolean isImage = false;
public boolean isParsingComplete() {
return parsingComplete;
}
public void setParsingComplete(boolean parsingComplete) {
this.parsingComplete = parsingComplete;
}
public void setTitle(String title) {
this.title = title;
}
public void setLink(String link) {
this.link = link;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getImageTitle() {
return imageTitle;
}
public void setImageTitle(String imageTitle) {
this.imageTitle = imageTitle;
}
public String getImageDescription() {
return imageDescription;
}
public void setImageDescription(String imageDescription) {
this.imageDescription = imageDescription;
}
public String getImageLink() {
return imageLink;
}
public void setImageLink(String imageLink) {
this.imageLink = imageLink;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getMedia_content() {
return media_content;
}
public void setMedia_content(String media_content) {
this.media_content = media_content;
}
public String getUrlString() {
return urlString;
}
public void setUrlString(String urlString) {
this.urlString = urlString;
}
public HandleXML(String url) {
this.urlString = url;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getDescription() {
return description;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (!isImage && name.equals("title")) {
title = text;
Log.e("Input text : ",text);
}
else if (!isImage && name.equals("link")) {
link = text;
Log.e("Input link : ",text);
}
else if (!isImage && name.equals("description")) {
description = text;
Log.e("Input description : ",text);
}
else if (name.equals("image")) {
image = text;
isImage = true;
Log.e("Input image : ",text);
}
else if (isImage && name.equals("title")) {
imageTitle = text;
Log.e("Input image title : ",text);
}
else if (isImage && name.equals("description")) {
imageDescription = text;
Log.e("Input image descr : ",text);
}
else if (isImage && name.equals("link")) {
imageLink = text;
Log.e("Input link : ",text);
}
else if (isImage && name.equals("guid")) {
guid = text;
Log.e("Input guid : ",text);
}
else if (isImage && name.equals("pubDate")) {
pubDate = text;
Log.e("Input pubdate : ",text);
}
else if (isImage && name.equals("media:content")) {
media_content = text;
// isImage = false;
Log.e("Input media:content : ",text);
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
}
}
});
thread.start();
}
}
如何獲取物品清單?請幫忙。
這表明ITEMLIST大小總是0項永遠不會在列表中添加。 –
請再次查看代碼段。我測試的項目將被添加到列表中。 – chancyWu