Reputation: 31
Here is the XML I'm trying to read through in Android Java using the XmlPullParser but I can't find a way to read the <link/>
tag since it's selfclosed tag
<id>
tag:google.com,2013:googlealerts/feed:10407958590599670710
</id>
<title type="html">
Uhuru's order on fare control has no legal backing
</title>
<link href="https://www.google.com/url?rct=j&sa=t&url=https://www.nation.co.ke/business/Uhuru-s-order-on-fare-control-has-no-legal-backing/996-4768072-coxlk6z/index.html&ct=ga&cd=CAIyHDI1YTNhOGJmZjY3ZmQ4NTk6Y29tOmVuOktFOlI&usg=AFQjCNG10EpkC5Gogga5T4Hkys8pg3TCHw"/>
<published>2018-09-19T18:11:15Z</published>
<updated>2018-09-19T18:11:15Z</updated>
<content type="html">
Lawyers, matatu operators and
<b>NTSA</b> sources said that the transport ... transport sector — said
<b>NTSA</b> has no legal mandate to set fares, adding that ...
</content>
<author>
<name/>
</author>
This is my java code snippet trying to archive reading through the xml
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("entry")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
titles.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem) {
links.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("content")) {
if (insideItem) {
content.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("published")) {
if (insideItem) {
published.add(xpp.nextText());
}
}
} else if (eventType == XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("entry")) {
insideItem = false;
}
eventType = xpp.next();
}
Upvotes: 0
Views: 395
Reputation: 31
I was able to read the self closed tag finally using this piece of code:
protected Exception doInBackground(Integer... integers) {
try {
url = new URL(RSS_URL_2);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("entry")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
titles.add(xpp.nextText());
}
} else if (xpp.getName().equals("link")) {
if (insideItem) {
links.add(xpp.getAttributeValue(null, "href"));
}
} else if (xpp.getName().equalsIgnoreCase("content")) {
if (insideItem) {
content.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("published")) {
if (insideItem) {
published.add(xpp.nextText());
}
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("entry")) {
insideItem = false;
}
eventType = xpp.next();
}
} catch (MalformedURLException me) {
exception = me;
} catch (XmlPullParserException xp) {
exception = xp;
} catch (IOException ie) {
exception = ie;
}
return exception;
}
Upvotes: 1