我正在使用enter link description here
我可以从URL获取所有必需的数据。但我想将<item>...</item>
属性作为List获取。我可以逐个获取所有属性。我想要项目属性的完整列表。
他们的任何其他选择是否相同?
这是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();
}
}
如何获取物品清单?请帮忙。
答案 0 :(得分:1)
你应该 1)将Item类作为模型与HandleXML分开。 2)在频道标签的开头,初始化一个项目列表。 3)在item标签的开头,创建一个新的item对象。 4)相应地填充Item对象。 5)在item标记的末尾,将Item对象添加到列表中。
你应该注意isImage
变量。 init应为true,一旦创建了一个item对象,则设置为false。
代码片段如下:
public class HandleXML {
public volatile boolean parsingComplete = true;
private String image = "image";
private String imageTitle = "title";
private String imageDescription = "description";
private String imageLink = "link";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
private boolean isImage = true; // isImage init to be true
private List<Item> itemList = null;
// separate the Item class with HandleXML
class Item {
private String title = "title";
private String link = "link";
private String description = "description";
private String guid = "guid";
private String pubDate = "pubDate";
private String media_content = "media:content";
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 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 getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getDescription() {
return description;
}
}
public boolean isParsingComplete() {
return parsingComplete;
}
public void setParsingComplete(boolean parsingComplete) {
this.parsingComplete = parsingComplete;
}
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 getUrlString() {
return urlString;
}
public void setUrlString(String urlString) {
this.urlString = urlString;
}
public HandleXML(String url) {
this.urlString = url;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text = null;
Item newItem = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
if (name.equalsIgnoreCase("channel")) {
// init the list for item.
itemList = new ArrayList<Item>();
}
if (name.equalsIgnoreCase("item")) {
// create a new item object.
newItem = new Item();
isImage = false; // isImage to false
}
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (!isImage && name.equals("title")) {
newItem.setTitle(text);
Log.e("Input text : ",text);
}
else if (!isImage && name.equals("link")) {
newItem.setLink(text);
Log.e("Input link : ",text);
}
else if (!isImage && name.equals("description")) {
newItem.setDescription(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")) {
newItem.setGuid(text);
Log.e("Input guid : ",text);
}
else if (!isImage && name.equals("pubDate")) {
newItem.setPubDate(text);
Log.e("Input pubdate : ",text);
}
else if (!isImage && name.equals("media:content")) {
newItem.setMedia_content(text);
// isImage = false;
Log.e("Input media:content : ",text);
}
else if (name.equalsIgnoreCase("item")) {
// add the item to list in the end of item tag
itemList.add(newItem);
Log.e("itemList", "Add one");
}
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, "UTF_8");
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
}
}
});
thread.start();
}
}