Friday, 2 June 2017

Abdera: Parse an atom document

Abdera provides parser api, to parse an atom document. Following step by step procedure explains how to parse an atom document using Abdera library.

Step 1: Get the instance of Abdera.
Abdera abdera = Abdera.getInstance();

Step 2: Get the parser instance from Abdera object
Parser parser = abdera.getParser();

Step 3: Connect to the url.
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip");
conn.connect();

Step 4: Get the input stream fro connection. If the response comes in gzip format, we should use GZIPInputStream, else InputStream is fine.

String contentEncoding = conn.getContentEncoding();
boolean isGzip = contentEncoding != null && contentEncoding.contains("gzip");
InputStream in = !isGzip ? conn.getInputStream() : new GZIPInputStream(conn.getInputStream())

Step 5: Parse the input stream and get the Document instance.
Document<Feed> doc = parser.parse(in, url.toString());

Step 6:  Get the Feed object from Document object and use the methods provided by Feed object to get the data.

Document<Feed> doc = parser.parse(in, url.toString());
Feed feed = doc.getRoot();

Following is the complete working application.

ParserDemo.java
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

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

public class ParserDemo {

 public static void parseURL(String urlStr) {
  HttpURLConnection conn = null;

  try {
   URL url = new URL(urlStr);
   conn = (HttpURLConnection) url.openConnection();
   conn.setRequestProperty("Accept-Encoding", "gzip");
   conn.connect();

   String contentEncoding = conn.getContentEncoding();
   boolean isGzip = contentEncoding != null && contentEncoding.contains("gzip");

   try (InputStream in = !isGzip ? conn.getInputStream() : new GZIPInputStream(conn.getInputStream())) {
    Abdera abdera = Abdera.getInstance();
    Parser parser = abdera.getParser();
    Document<Feed> doc = parser.parse(in, url.toString());
    Feed feed = doc.getRoot();
    System.out.println(feed.getTitle());
    for (Entry entry : feed.getEntries()) {
     System.out.println("\t" + entry.getTitle());
    }
    System.out.println(feed.getAuthor());
   }

  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   if (conn != null) {
    conn.disconnect();
   }
  }

 }

 public static void main(String args[]) throws ParseException, IOException {
  parseURL("https://self-learning-java-tutorial.blogspot.com/atom.xml");
 }
}


Output
java tutorial : Blog to learn java programming
 SWT: Scale widget
 Commons CLI: OptionGroup: group mutually Exclusive options
 Commons CLI: Parsing command line options
 Commons CLI: Print command line argument in the order you inserted
 Commons-CLI: Setting the compulsory options
 Commons CLI: Add multiple arguments to an option
 Commons CLI: Boolean options
 Commons CLI : Options class
 Commons-CLI Define options using builder
 Commons-CLI: Set the display name for the argument
 Commons CLI: Option: Specify single command line option
 Apache Commons CLI Tutorial
 SWT: Slider
 C#: zeroMQ: Publish-subscribe model sockets
 C#: Get the version of zeroMQ used in the application
 zeroMQ: Hello World Application
 zeroMQ C# Tutorial
 SWT: TabFolder widget
 SWT: Table widget
 Client in C# and Server in Java 
 Two way communication in C# client and socket
 C#:  Socket Programming
 SWT: Focus listeners
 SWT: Text listeners
 SWT: Mouse listeners
<author xmlns="http://www.w3.org/2005/Atom"><name>hari krishna</name><email>noreply@blogger.com</email><gd:image xmlns:gd="http://schemas.google.com/g/2005" rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-6kwxUAiFEDA/AAAAAAAAAAI/AAAAAAAADTE/LmWjnEaeU5c/s512-c/photo.jpg"/></author>





Previous                                                 Next                                                 Home

No comments:

Post a Comment