This is an example RSS feed.
http://na.leagueoflegends.com/en/rss.xml
<channel>
<title>Most Recent News</title>
<link>http://na.leagueoflegends.com/en/rss.xml</link>
<description/>
<language>en-US</language>
<item>
<title>Community-recommended bundles available now </title>
<link>http://na.leagueoflegends.com/en/news/store/sales/community-recommended-bundles-available-now?ref=rss</link>
<description><div class="field field-name-field-body-small field-type-text-long field-label-hidden">Craving something new at champ select? Get fed on these limited-time bundles based on players’ suggestions!</div><div class="field field-name-field-article-media field-type-file field-label-hidden"><div id="file-11675" class="file file-image file-image-jpeg"> <img typeof="foaf:Image" src="/sites/default/files/styles/large/public/upload/bundle-banner_1.jpg?itok=4MD_leb5" width="480" height="270" alt="" title="" /></div></div><div class="field field-name-custom-author field-type-entityreference field-label-hidden"><span class="article_detail"><span class="posted_by">By WizardCrab</span></span></div></description>
<pubDate>Mon, 30 Jun 2014 19:35:11 +0000</pubDate>
<dc:creator>sbroome</dc:creator>
<guid isPermaLink="false">18268 at http://na.leagueoflegends.com</guid>
</item>
<item>
<title>Champion and skin sale: 07.04 - 07.07</title>
<link>http://na.leagueoflegends.com/en/news/store/sales/champion-and-skin-sale-0704-0707?ref=rss</link>
<description><div class="field field-name-field-body-small field-type-text-long field-label-hidden">Grab these champions and skins on sale for 50% off for a limited time:</div><div class="field field-name-field-article-media field-type-file field-label-hidden"><div id="file-13873" class="file file-image file-image-jpeg"> <img typeof="foaf:Image" src="/sites/default/files/styles/large/public/upload/mu842qnf.jpg?itok=OktrEQdQ" width="480" height="270" alt="" title="" /></div></div><div class="field field-name-custom-author field-type-entityreference field-label-hidden"><span class="article_detail"><span class="posted_by">By WizardCrab</span></span></div></description>
<pubDate>Tue, 24 Jun 2014 21:24:10 +0000</pubDate>
<dc:creator>sbroome</dc:creator>
<guid isPermaLink="false">18060 at http://na.leagueoflegends.com</guid>
</item>
<item>
....
....
.... (and goes on for a while)
I wanted to get eat attribute within the .
I am currently using
URL rssFeedUrl = new URL(rssUrl);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
RSSHandler rssHandler = new RSSHandler();
xmlReader.setContentHandler(rssHandler);
InputSource inputSource = new InputSource(rssFeedUrl.openStream());
xmlReader.parse(inputSource);
private class RSSHandler extends DefaultHandler {
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
if (localName.equals("item")) {
item = true;
}
if(item) {
if(localName.equals(RSS_TITLE)) {
} else if(localName.equals(RSS_LINK)) {
} else if(localName.equals(RSS_DESCRIPTION)) {
} else if(localName.equals(RSS_PUB_DATE)) {
} else if(localName.equals(RSS_CREATOR)) {
} else if(localName.equals(RSS_GUID)) {
}
}
if (!localName.equals("item") && item == true) {
rssResult = rssResult + "
" + localName + ": ";
}
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
}
public void characters(char[] ch, int start, int length) throws SAXException {
String cdata = new String(ch, start, length);
if (item == true) {
String x = (cdata.trim()).replaceAll("\s+", " ");
rssResult = rssResult + x + "";
}
}
Is there a simpler way? Cause right now it is too tedious. I basically want to say getElement("description")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…