Showing posts with label Abdera Atom feed parser eample. Show all posts
Showing posts with label Abdera Atom feed parser eample. Show all posts

Saturday, 20 October 2012

Abdera Atom feed parser eample


//sample code to parse a atom feed by passing blogs/profiles/community xmls requires abdera jar files

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.ParseException;
import org.apache.abdera.parser.Parser;

public class Test {

    private static void readTags(String path) {
        System.out.println("Parsing File:" + path);
        System.out.println(" getting updera");
        Abdera abdera = new Abdera();
        Parser parser = abdera.getParser();
        URL localFileURL;
        Document<Feed> commDom = null;
        try {
            localFileURL = new URL(new File(path).toURI().toString());//getting our file
            commDom = parser.parse(localFileURL.openStream());//passing xml as a stream
        } catch (NoClassDefFoundError e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Feed feed = commDom.getRoot();//getting the feed root element
        List<Entry> requiredEntries = feed.getEntries();//getting entries in a feed
        for (Entry entry : requiredEntries) {
            System.out.println("-- starting of entry --");
            List<Element> elements = entry.getElements();
            for (Element allEntries : elements) {
                if (allEntries.getQName().getLocalPart().equals("link")) {//searching for our match element
                    String attrValue=allEntries.getAttributeValue("rel");
                    if(attrValue.equals("alternate"))
                    System.out.println("link hai -> " + allEntries.getAttributeValue("href"));
                }
                if (allEntries.getQName().getLocalPart().equals("rank")) {
                    System.out.println("individual scheme values"+allEntries.getAttributeValue("scheme"));
                    if(allEntries.getAttributeValue("scheme").contains("hit")){
                    System.out.println("number of visits:-"+allEntries.getText()); //prints no.of visits  
                    }
                    else if(allEntries.getAttributeValue("scheme").contains("comment")){
                    System.out.println("number of comments:-"+allEntries.getText());   //prints no.of comments
                    }
                    //blog.put("summary", allEntries.getText());
                }
                if (allEntries.getQName().getLocalPart().equals("summary")) {
                    System.out.println("summary -> " + allEntries.getText());//prints the summary
                }
               
                if (allEntries.getQName().getLocalPart().equals("author")) {
                    List<Element> objectElements = allEntries.getElements();
                    for (Element allElements : objectElements) {
                        if (allElements.getQName().getLocalPart()
                                .equals("name")) {
                            System.out.println("author  -> "
                                    + allElements.getText());//printing content inside the element
                        }
                    }
                }
            }
            System.out.println("-- ending of entry --");
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String profileTagsPath = "D:\\blogs.xml"; //this can be any atom feed xml given by connections
        readTags(profileTagsPath);
        System.out.println("parsing completed");
    }

}

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...