问题
I am making an reader that will read RSS Feeds. At first I wanted to use a library but realized they were failing to load some data so I decided to make my own reader. But here is the issue. It is not always that my parser returns an image, depending on the site like for this RSS The image is in the content:data while for RSS like that of BBC the image is found in the content:data I fail to get in the first iteration So i have the next item getting the image of the previous item, below is my code String title = "";
String description = "";
String link = "";
String encodedDesciption = "";
String imgSrc = "image";
boolean isItem = false;
try {
XmlPullParser xmlPullParser = Xml.newPullParser();
xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
xmlPullParser.setInput(inputStream, null);
// xmlPullParser.nextTag();
while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {
int eventType = xmlPullParser.getEventType();
String name = xmlPullParser.getName();
if(name == null)
continue;
if(eventType == XmlPullParser.END_TAG) {
if(name.equalsIgnoreCase("item")) {
isItem = false;
}
continue;
}
if (eventType == XmlPullParser.START_TAG) {
if(name.equalsIgnoreCase("item")) {
isItem = true;
continue;
}
}
String result = "";
if (xmlPullParser.next() == XmlPullParser.TEXT) {
result = xmlPullParser.getText();
xmlPullParser.nextTag();
}
if (name.equalsIgnoreCase(TITLE)) {
title = result;
} else if (name.equalsIgnoreCase(LINK)) {
link = result;
} else if (name.equalsIgnoreCase(DESCRIPTION)) {
description = result;
}else if (name.equalsIgnoreCase(DESCRIPTION_ENCODED)) {
encodedDesciption = result;
}
if (title != null && link != null && description != null && imgSrc != null && encodedDesciption != null) {
if(isItem) {
Document document = Jsoup.parse(encodedDesciption);
Element img = document.select("img").first();
if (img != null) {
imgSrc = img.attr("src");
Log.d(TAG, "Image found "+imgSrc);
img.remove();
}else Log.d(TAG, "Image not found");
FeedItem feedItem = new FeedItem(title, description, encodedDesciption, imgSrc, link );
feeds.add(feedItem);
Log.d(TAG, feedItem.toString());
}
title = null;
link = null;
description = null;
isItem = false;
}
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
How do I get the right content:data
at the right index? and how do I get the url in
来源:https://stackoverflow.com/questions/44772318/android-xml-parser-skipping-tags