Google owned blogging platform www.blogger.com providing a free way to manage blogs. It is following atom feed to provide sitemaps to search engines. This blog is also from blogger platform. You can submit this sitemap URL to google web master tools to have better SEO. Ok, now we will see how to generate Atom feed for Blogger blog. Observe below syntax

Syntax

start-index and max-results will decide the atom feed content
<blog url>/atom.xml?redirect=false&start-index=1&max-results=50
Examples
Click on below link to get latest 3 articles feed
http://blog.sodhanalibrary.com/atom.xml?redirect=false&start-index=1&max-results=3
Click on below link to get 5th latest article feed
http://blog.sodhanalibrary.com/atom.xml?redirect=false&start-index=5&max-results=1

XML Structure

Atom feed gives data in XML format. Find below for format 
<feed ----->
  -----
  <entry>
     -----
     <title>----</title>
     <category term="---"/>
     <category term="---"/>
     <content ---->
        ----
     </content>
  </entry>
</feed>

Java Program

Lets move to java program. We need to read atom feed xml nodes to get the required data. The below example is for reading article titles and their tags. Observe below java program. This program reads all articles titles and tags of this blog
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class BloggerReader {
    static int FEED_COUNT = 50;
    public static void main(String[] args) {
        try {
            int START_INDEX = 1; 
            while (true) {
                // Atom feed URL
                URL url = new URL(
                        "http://blog.sodhanalibrary.com/atom.xml?redirect=false&start-index="+START_INDEX+"&max-results="
                                + FEED_COUNT);
                InputStream input = url.openStream();

                // XPATH to get entries
                DocumentBuilderFactory factory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(input);
                XPathFactory xPathfactory = XPathFactory.newInstance();
                XPath xpath = xPathfactory.newXPath();
                XPathExpression expr = xpath.compile("/feed/entry");

                // Get Nodelist of entries
                NodeList nl = (NodeList) expr.evaluate(doc,
                        XPathConstants.NODESET);

                // iterate Nodelist
                for (int i = 0; i < nl.getLength(); i++) {
                    NodeList nl1 = nl.item(i).getChildNodes();

                    // iterate child nodes
                    for (int j = 0; j < nl1.getLength(); j++) {
                        Node node = nl1.item(j);
                        if (node.getNodeName().equals("title")) {
                            // print title
                            System.out.println(node.getTextContent());
                        } else if (node.getNodeName().equals("category")) {
                            // print categories
                            System.out.println(node.getAttributes()
                                    .getNamedItem("term").getTextContent());
                        }
                    }
                }
                
                START_INDEX += FEED_COUNT;
                if(FEED_COUNT > nl.getLength()) {
                  break;
                };
            }
        } catch (SAXParseException err) {
            err.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

7 comments:

  1. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this van rental montreal amazing blog. I am always searching like this type blog post. I hope I will see again.

    ReplyDelete
  2. Good evening guys! Don't waste a lot of time for boring research and stupid topics. I have one advice just use the best paper writing service masterpapers.com. I always used during my university period and never felt nervous about grades. Studying can be so much easier when you know who you should ask for help.

    ReplyDelete
  3. Here data.expanded variable which makes Accordion collapse and expand. Whenever user clicks on it, this variable will be toggled! Not every student will require help with their nursing research paper. However, for important papers, it is often best to have your work edited carefully before you submit to ensure that your work will be worthy of the best grades.

    ReplyDelete
  4. Pretty useful material in your blog almost for all students, and they can simultaneously explore something new. The Best Essay Service Overview personal statement service. Continue in the same spirit, performing excellent work that will be a great help for you.

    ReplyDelete
  5. Furthermore, our plagiarism software tool is consistently upgraded ( https://jetwriting.com/write-my-dissertation/ ) to ensure that it detects plagiarized texts with high certainty and accuracy.

    ReplyDelete
  6. There is nothing wrong with rewarding yourself or taking a break, on this site https://optimisticmommy.com/hacks-to-help-improve-your-health-and-lifestyle-in-2021/ when it comes to making a healthier lifestyle a habit, consistency every day benefits you in the long run.

    ReplyDelete

Blogroll

Popular Posts