Created
September 20, 2011 17:53
-
-
Save joshbirk/1229785 to your computer and use it in GitHub Desktop.
Converting an RSS Feed into a Custom Object
This file contains 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
static global List<Dom.XMLNode> getRSSFeed(string URL) { | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
// url that returns the XML in the response body | |
req.setEndpoint(url); | |
req.setMethod('GET'); | |
HttpResponse res = h.send(req); | |
Dom.Document doc = res.getBodyDocument(); | |
Dom.XMLNode rss = doc.getRootElement(); | |
System.debug('@@' + rss.getName()); | |
List<Dom.XMLNode> rssList = new List<Dom.XMLNode>(); | |
for(Dom.XMLNode child : rss.getChildren()) { | |
System.debug('@@@' + child.getName()); | |
for(Dom.XMLNode channel : child.getChildren()) { | |
System.debug('@@@@' + channel.getName()); | |
if(channel.getName() == 'item') { | |
rssList.add(channel); | |
} | |
} | |
} | |
return rssList; | |
} | |
static global Blog_Entry__c convertFeedToBlogPost(Dom.XMLNode post) { | |
Blog_Entry__c bp = new Blog_Entry__c(); | |
Integer tagIndex = 0; | |
for(Dom.XMLNode child : post.getChildren()) { | |
// System.debug('@@@@ -- @' + child.getName()); | |
if(child.getName() == 'title') { bp.Title__c = child.getText(); } | |
if(child.getName() == 'creator') { bp.Author__c = child.getText(); } | |
if(child.getName() == 'pubDate') { bp.Published__c = Utility.convertRSSDateStringToDate(child.getText()); } | |
if(child.getName() == 'origLink') { bp.Link__c = child.getText(); } | |
if(child.getName() == 'description') { bp.Lead_Copy__c = child.getText(); } | |
if(child.getName() == 'category') { | |
tagIndex++; | |
if(tagIndex == 1) { bp.Tag_1__c = child.getText(); } | |
if(tagIndex == 2) { bp.Tag_2__c = child.getText(); } | |
if(tagIndex == 3) { bp.Tag_3__c = child.getText(); } | |
} | |
} | |
return bp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment