Forum Discussion

dchou's avatar
7 years ago

Parse RSS Feed

I would like to retrieve data from our company website RSS and parse that in an endpoint.I am able to get the data from RSS but don't know the exact way to parse RSS in freemarker.

 

Please give me a suggestion.

2 Replies

  • dchou - You can use below http post call:

     

    <#assign http_client_request = http.client.request("http", "rss feed url").get() />
    <#if http_client_request.hasError>
    ${http_client_request.error.message}
    <#else>
    <#assign result = http_client_request.content />
    ${result}
    </#if>

     

    Thanks,

    Payal

  • dchou

    If you want to know the way to get the feed in endpoint so you can follow the Payal's reply. But as you mentioned you are able to get the feed but do not know how to parse. Can I know what you are trying to do so we can help you better? 

    A couple of months back, I was doing the same stuff, getting the feed from WordPress and posting them in the community.  If you are on the same page then follow below steps. 

    1. Get the data from RSS feed in endpoint as mentioned by Payal

    2. Call this endpoint from component so the feed can be passed on component and you can parse it easily in JS instead of freemarker. 

    3. Once the result is in the component then parse or segregate the data in the component as I did in given snippet.  i.e. 

    $(result).find("item").each(function(index){
    // Items (like subject, body, date et) segregation goes here
    })

     
    Below snippet can help to get it done in component

     

    $.ajax({
                url: "/plugins/custom/community/community/blog-content-ep",
                success: function(result) {
                var data;
                var body;
                        $(result).find("item").each(function(index){
                            var el=$(this);
                            var subject=el.find("title").text();
                            if($.inArray(subject, importedItemsArr) < 0) {
                            importableMsgCount++;
                            var contentEl = el.find("content:encoded").prevObject[0].children;
                            for(i=0;i<contentEl.length;i++){
                                if(contentEl[i].nodeName=="CONTENT:ENCODED"){
                                    body=contentEl[i].innerHTML;
                                    break;
                                }
                            }
                            post_time = el.find("pubDate").text();
                            month = post_time.substring(8,11);
                            day = post_time.substring(5,7);
                            year = post_time.substring(12,16);
                            hour = parseInt(post_time.substring(17,19));
                            AmPm = " AM";
                            if(hour>12) {
                                hour=hour-12;
                                AmPm = " PM";
                            }
                            minute = post_time.substring(20,22);
                            sec = post_time.substring(23,25);
                            full_date = month+" "+day+", "+year+" "+hour+":"+minute+":"+sec+AmPm;
                            data={
                                "subject": subject,
                                "post_time": full_date,
                                "body": body,
                                "author": '${author}',
                                "blog_id":board_id
                            } 
                            $.ajax({
                            type: 'POST',
                            async:false,
                            url: "/plugins/custom/community/community/add-blog-ep",
                            data: data,
                            success: function(data){
                            // Successfully posted in community
                            }
                        })  
                        }
                    })
                }
    });