Last active
January 20, 2020 08:30
-
-
Save macsystems/01d7e80554efd344b1f9 to your computer and use it in GitHub Desktop.
If you want to parse an RSS Feed using SimpleXML you can use this as an Start. I used this to parse RSS using RetroFit from Square
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.simpleframework.xml.Attribute; | |
import org.simpleframework.xml.Element; | |
import org.simpleframework.xml.ElementList; | |
import org.simpleframework.xml.Namespace; | |
import org.simpleframework.xml.NamespaceList; | |
import org.simpleframework.xml.Root; | |
import org.simpleframework.xml.Text; | |
import java.util.List; | |
@NamespaceList({ | |
@Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom") | |
}) | |
@Root(strict = false) | |
public class Channel { | |
// Tricky part in Simple XML because the link is named twice | |
@ElementList(entry = "link", inline = true, required = false) | |
public List<Link> links; | |
@ElementList(name = "item", required = true, inline = true) | |
public List<Item> itemList; | |
@Element | |
String title; | |
@Element | |
String language; | |
@Element(name = "ttl", required = false) | |
int ttl; | |
@Element(name = "pubDate", required = false) | |
String pubDate; | |
@Override | |
public String toString() { | |
return "Channel{" + | |
"links=" + links + | |
", itemList=" + itemList + | |
", title='" + title + '\'' + | |
", language='" + language + '\'' + | |
", ttl=" + ttl + | |
", pubDate='" + pubDate + '\'' + | |
'}'; | |
} | |
public static class Link { | |
@Attribute(required = false) | |
public String href; | |
@Attribute(required = false) | |
public String rel; | |
@Attribute(name = "type", required = false) | |
public String contentType; | |
@Text(required = false) | |
public String link; | |
} | |
@Root(name = "item", strict = false) | |
public static class Item { | |
@Element(name = "title", required = true) | |
String title;//The title of the item. Venice Film Festival Tries to Quit Sinking | |
@Element(name = "link", required = true) | |
String link;//The URL of the item. http://www.nytimes.com/2002/09/07/movies/07FEST.html | |
@Element(name = "description", required = true) | |
String description;//The item synopsis. Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged. | |
@Element(name = "author", required = false) | |
String author;//Email address of the author of the item. More. [email protected] | |
@Element(name = "category", required = false) | |
String category;//Includes the item in one or more categories. More. Simpsons Characters | |
@Element(name = "comments", required = false) | |
String comments;//URL of a page for comments relating to the item. More. http://www.myblog.org/cgi-local/mt/mt-comments.cgi?entry_id=290 | |
@Element(name = "enclosure", required = false) | |
String enclosure;// Describes a media object that is attached to the item. More. <enclosure url="http://live.curry.com/mp3/celebritySCms.mp3" length="1069871" type="audio/mpeg"/> | |
@Element(name = "guid", required = false) | |
String guid;//A string that uniquely identifies the item. More. <guid isPermaLink="true">http://inessential.com/2002/09/01.php#a2</guid> | |
@Element(name = "pubDate", required = false) | |
String pubDate;// Indicates when the item was published. More. Sun, 19 May 2002 15:21:36 GMT | |
@Element(name = "source", required = false) | |
String source;// The RSS channel that the item came from. More. | |
@Override | |
public String toString() { | |
return "Item{" + | |
"title='" + title + '\'' + | |
", link='" + link + '\'' + | |
", description='" + description + '\'' + | |
", author='" + author + '\'' + | |
", category='" + category + '\'' + | |
", comments='" + comments + '\'' + | |
", enclosure='" + enclosure + '\'' + | |
", guid='" + guid + '\'' + | |
", pubDate='" + pubDate + '\'' + | |
", source='" + source + '\'' + | |
'}'; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <T> T createXmlAdapterFor(final Class<T> api, final String endpoint, final Client client) { | |
Preconditions.checkNotNull(client); | |
Preconditions.checkNotNull(endpoint); | |
// | |
final RestAdapter.LogLevel level = getLogLevel(); | |
Log.d(LOG_TAG, "Log Level:" + level); | |
final RestAdapter adapter = new RestAdapter.Builder()// | |
.setEndpoint(endpoint)// | |
.setConverter(new SimpleXMLConverter())// | |
.setClient(client)// | |
.setLogLevel(level)// | |
.build(); | |
// | |
return adapter.create(api); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.simpleframework.xml.Attribute; | |
import org.simpleframework.xml.Element; | |
import org.simpleframework.xml.Root; | |
@Root | |
public class RSS { | |
@Attribute | |
String version; | |
@Element | |
Channel channel; | |
public Channel getChannel() { | |
return channel; | |
} | |
@Override | |
public String toString() { | |
return "RSS{" + | |
"version='" + version + '\'' + | |
", channel=" + channel + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final String baseURL = getString(YOU_ENDPOINT_URL); | |
final Client client = RestAdapterUtil.createOKClient(); | |
endpoint = RestAdapterUtil.createXmlAdapterFor(Endpoint.class, baseURL, client); | |
final Observable<RSS> events = endpoint.getRSSFeed(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@macsystems I have added fields for the popular rss content module. Maybe they will be useful for other people as well and you might want to add them here (since PRs are not possible for gists)