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();
        }
    }
}

0 comments:

Blogroll

Follow this blog by Email

Popular Posts