Skip to content

Instantly share code, notes, and snippets.

@tschuchortdev
Forked from macsystems/Channel.java
Last active January 25, 2018 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschuchortdev/1d43f24bbb7978e2460a61dc2fac01b5 to your computer and use it in GitHub Desktop.
Save tschuchortdev/1d43f24bbb7978e2460a61dc2fac01b5 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
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;
@Namespace(prefix = "content", reference = "http://purl.org/rss/1.0/modules/content/")
@Element(name = "encoded", data = true, required = false)
String content; // content namespace extension. Can contain the actual contain in a CDATA tag, which would not be otherwise allowed
@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. oprah@oxygen.net
@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.
@Namespace(prefix = "content", reference = "http://purl.org/rss/1.0/modules/content/")
@Element(name = "encoded", data = true, required = false)
String content; // content namespace extension. Can contain the actual contain in a CDATA tag, which would not be otherwise allowed
@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 + '\'' +
'}';
}
}
}
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);
}
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 +
'}';
}
}
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