Friday, 15 April 2011

Reading/Parsing RSS feed using ROME

ROME is an open source tool to parse, generate and publish RSS and Atom feeds. Using Rome you can parse the available RSS and Atom feeds. Without bothering about format and version of RSS feed. The core library depends on the JDOM XML parser.
Atom is on the similar lines of RSS is another kind of feed. But it’s different in some aspects as protocol, payloads.
RSS is a method to share and publish contents. The contents may be any things from news to any little information. The main component is xml. Using xml you can share your contents on web. At the same time you are free to get what you like from others.

Why use Rome instead of other available readers

The Rome project started with the motivation of ‘ESCAPE’ where each letter stands for:
E – Easy to use. Just give a URL and forget about its type and version, you will be given a output in the format which you like.
S – Simple. Simple structure. The complications are all hidden from developers.
C – Complete. It handles all the versions of RSS and Atom feeds.
A – Abstract. It provides abstraction over various syndication specifications.
P – Powerful. Don’t worry about the format let Rome handle it.
E – Extensible. It needs a simple pluggable architecture to provide future extension of formats.

Dependency

Following are few dependencies:
J2SE 1.4+, JDOM 1.0, Jar files (rome-0.8.jar, purl-org-content-0.3.jar, jdom.jar)

Using Rome to read a Syndication Feed

Considering you have all the required jar files we will start with reading the RSS feed. ROME represents syndication feeds (RSS and Atom) as instances of the com.sun.syndication.synd.SyndFeed interface.
ROME includes parsers to process syndication feeds into SyndFeed instances. The SyndFeedInput class handles the parsers using the correct one based on the syndication feed being processed. The developer does not need to worry about selecting the right parser for a syndication feed, the SyndFeedInput will take care of it by peeking at the syndication feed structure. All it takes to read a syndication feed using ROME are the following 2 lines of code:
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build (new XmlReader (feedUrl));
Now it’s simple to get the details of Feed. You have the object.

The sample code is as follows.
import java.net.URL;
import java.util.Iterator;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * @author Hanumant Shikhare
 */
public class Reader {

public static void main(String[] args) throws Exception {

URL url = new URL("http://viralpatel.net/blogs/feed");
XmlReader reader = null;

try {

reader = new XmlReader(url);
SyndFeed feed = new SyndFeedInput().build(reader);
System.out.println("Feed Title: "+ feed.getAuthor());

for (Iterator i = feed.getEntries().iterator(); i.hasNext();) {
SyndEntry entry = (SyndEntry) i.next();
System.out.println(entry.getTitle());
}
} finally {
if (reader != null)
reader.close();
}
}
}

Understanding the Program

Initialize the URL object with the RSS Feed or Atom url. Then we will need XMLReader object which will then take URL object, as its constructor argument. Initialize the SyndFeed object by calling the build(reader) method. This method takes the XMLReader object as an argument.

References

https://rome.dev.java.net/
http://www.intertwingly.net/wiki/pie/Rss20AndAtom10Compared
http://www.rss-specifications.com

Tuesday, 15 March 2011

Streaming API for XML (StaX)

1. Overview

Streaming API for XML, called StaX, is an API for reading and writing XML Documents.
StaX is a Pull-Parsing model. Application can take the control over parsing the XML documents by pulling (taking) the events from the parser.
The core StaX API falls into two categories and they are listed below. They are
  • Cursor API
  • Event Iterator API
Applications can any of these two API for parsing XML documents. The following will focus on the event iterator API as I consider it more convenient to use.

2. Event Iterator API

The event iterator API has two main interfaces: XMLEventReader for parsing XML and XMLEventWriter for generating XML.

XMLEventReader - Read XML file using STAX

Applications loop over the entire document requesting for the Next Event. The Event Iterator API is implemented on top of Cursor API.
In this example we will read the following XML document and create objects from it.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<item date="January 2009">
<mode>1</mode>
<unit>900</unit>
<current>1</current>
<interactive>1</interactive>
</item>
<item date="February 2009">
<mode>2</mode>
<unit>400</unit>
<current>2</current>
<interactive>5</interactive>
</item>
<item date="December 2009">
<mode>9</mode>
<unit>5</unit>
<current>100</current>
<interactive>3</interactive>
</item>
</config>
This the pojo class which holds the information stored by above xml file:
public class Item {
private String date;
private String mode;
private String unit;
private String current;
private String interactive;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
this.mode = mode;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getCurrent() {
return current;
}
public void setCurrent(String current) {
this.current = current;
}
public String getInteractive() {
return interactive;
}
public void setInteractive(String interactive) {
this.interactive = interactive;
}

@Override
public String toString() {
return "Item [current=" + current + ", date=" + date + ", interactive="
+ interactive + ", mode=" + mode + ", unit=" + unit + "]";
}
}
The following reads the XML file and creates a List of object Items from the entries in the XML file.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import de.vogella.xml.stax.model.Item;

public class StaXParser {
static final String DATE = "date";
static final String ITEM = "item";
static final String MODE = "mode";
static final String UNIT = "unit";
static final String CURRENT = "current";
static final String INTERACTIVE = "interactive";

@SuppressWarnings({ "unchecked", "null" })
public List<Item> readConfig(String configFile) {
List<Item> items = new ArrayList<Item>();
try {
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = new FileInputStream(configFile);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// Read the XML document
Item item = null;

while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();

if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
// If we have a item element we create a new item
if (startElement.getName().getLocalPart() == (ITEM)) {
item = new Item();
// We read the attributes from this tag and add the date
// attribute to our object
Iterator<Attribute> attributes = startElement
.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
if (attribute.getName().toString().equals(DATE)) {
item.setDate(attribute.getValue());
}

}
}

if (event.isStartElement()) {
if (event.asStartElement().getName().getLocalPart()
.equals(MODE)) {
event = eventReader.nextEvent();
item.setMode(event.asCharacters().getData());
continue;
}
}
if (event.asStartElement().getName().getLocalPart()
.equals(UNIT)) {
event = eventReader.nextEvent();
item.setUnit(event.asCharacters().getData());
continue;
}

if (event.asStartElement().getName().getLocalPart()
.equals(CURRENT)) {
event = eventReader.nextEvent();
item.setCurrent(event.asCharacters().getData());
continue;
}

if (event.asStartElement().getName().getLocalPart()
.equals(INTERACTIVE)) {
event = eventReader.nextEvent();
item.setInteractive(event.asCharacters().getData());
continue;
}
}
// If we reach the end of an item element we add it to the list
if (event.isEndElement()) {
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart() == (ITEM)) {
items.add(item);
}
}

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
return items;
}

}

Testing the program:
import java.util.List;

import com.vani.xml.stax.model.Item;

public class TestRead {
public static void main(String args[]) {
StaXParser read = new StaXParser();
List<Item> readConfig = read.readConfig("config.xml");
for (Item item : readConfig) {
System.out.println(item);
}
}
}

JDOM (index)


Using JDOM to read a web.xml file

Now let's see JDOM in action by looking at how you could use it to parse a web.xml file, the Web application deployment descriptor from Servlet API 2.2. Let's assume that you want to look at the Web application to see which servlets have been registered, how many init parameters each servlet has, what security roles are defined, and whether or not the Web application is marked as distributed.
Here's a sample web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>file</servlet-name>
<servlet-class>ViewFile</servlet-class>
<init-param>
<param-name>initial</param-name>
<param-value>1000</param-value>
<description>
The initial value for the counter <!-- optional -->
</description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mv</servlet-name>
<url-pattern>*.wm</url-pattern>
</servlet-mapping>
<distributed/>
<security-role>
<role-name>manager</role-name>
<role-name>director</role-name>
<role-name>president</role-name>
</security-role>
</web-app>


On processing that file, you'd want to get output that looks like this:
This WAR has 2 registered servlets:
snoop for SnoopServlet (it has 0 init params)
file for ViewFile (it has 1 init params)
This WAR contains 3 roles:
manager
director
president
This WAR is distributed


With JDOM, achieving that output is easy. The following example reads the WAR file, builds a JDOM document representation in memory, then extracts the pertinent information from it:
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class WarReader {
public static void main(String[] args) {
PrintStream out = System.out;
if (args.length != 1 && args.length != 2) {
out.println("Usage: WarReader [web.xml]");
return;
}
try {
// Request document building without validation
SAXBuilder builder = new SAXBuilder(false);
Document doc = builder.build(new File(args[0]));
// Get the root element
Element root = doc.getRootElement();
// Print servlet information
List servlets = root.getChildren("servlet");
out.println("This WAR has "+ servlets.size() +" registered servlets:");
Iterator i = servlets.iterator();
while (i.hasNext()) {
Element servlet = (Element) i.next();
out.print("\t" + servlet.getChild("servlet-name")
.getText() +
" for " + servlet.getChild("servlet-class")
.getText());
List initParams = servlet.getChildren("init-param");
out.println(" (it has " + initParams.size() + " init params)");
}
// Print security role information
List securityRoles = root.getChildren("security-role");
if (securityRoles.size() == 0) {
out.println("This WAR contains no roles");
}
else {
Element securityRole = (Element) securityRoles.get(0);
List roleNames = securityRole.getChildren("role-name");
out.println("This WAR contains " + roleNames.size() + " roles:");
i = roleNames.iterator();
while (i.hasNext()) {
Element e = (Element) i.next();
out.println("\t" + e.getText());
}
}
// Print distributed information (notice this is out of order)
List distrib = root.getChildren("distributed");
if (distrib.size() == 0) {
out.println("This WAR is not distributed");
} else {
out.println("This WAR is distributed");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Reading the element data in jdom

Getting the root element
Every XML document must have a root element. That element is the starting point for accessing all the information within the document. For example, that snippet of a document has <root-demo> as the root:
<root-demo id="demo">
<description>Gotta fit servlets in somewhere!</description>
<distributable/>
</root-demo>
root-demo is the root of this xml document.

The root Element instance is available on a Document directly:
Element webapp = doc.getRootElement();

Getting the children
You can obtain an Element's children with various methods. getChild() returns null if no child by that name exists.
List getChildren(); // return all children
List getChildren(String name); // return all children by name
Element getChild(String name); // return first child by name


To demonstrate:

// Get a List of all direct children as Element objects
List allChildren = element.getChildren();
out.println("First kid: " + ((Element)allChildren.get(0)).getName());
// Get a list of all direct children with a given name
List namedChildren = element.getChildren("name");
// Get a list of the first kid with a given name
Element kid = element.getChild("name");

Using getChild() makes it easy to quickly access nested elements when the structure of the XML document is known in advance. Given that XML:
<?xml version="1.0"?>
<linux:config>
<gui>
<window-manager>
<name>Enlightenment</name>
<version>0.16.2</version>
</window-manager>
<!-- etc -->
</gui>
</linux:config>


That code directly retrieves the current window manager name:
String windowManager = rootElement.getChild("gui")
.getChild("window-manager")
.getChild("name")
.getText();


Just be careful about NullPointerExceptions if the document has not been validated. For simpler document navigation, future JDOM versions are likely to support XPath references. Children can get their parent using getParent().

Getting the element attributes
<table width="100%" border="0">  </table>


Those attributes are directly available on an Element.
String width = table.getAttributeValue("width");


You can also retrieve the attribute as an Attribute instance. That ability helps JDOM support advanced concepts such as Attributes residing in a namespace. (See the section Namespaces later in the article for more information.)
Attribute widthAttrib = table.getAttribute("width");
String width = widthAttrib.getValue();


For convenience you can retrieve attributes as various primitive types.
int width = table.getAttribute("border").getIntValue();


You can retrieve the value as any Java primitive type. If the attribute cannot be converted to the primitive type, a DataConversionException is thrown. If the attribute does not exist, then the getAttribute() call returns null.

Extracting element content

We touched on getting element content earlier, and showed how easy it is to extract an element's text content using element.getText(). That is the standard case, useful for elements that look like this:
<name>Enlightenment</name>


But sometimes an element can contain comments, text content, and child elements. It may even contain, in advanced documents, a processing instruction:
<table>
<!-- Some comment -->
Some text
<tr>Some child</tr>
<?pi Some processing instruction?>
</table>


This isn't a big deal. You can retrieve text and children as always:
String text = table.getText(); // "Some text"
Element tr = table.getChild("tr"); // <tr> child


That keeps the standard uses simple. Sometimes as when writing output, it's important to get all the content of an Element in the right order. For that you can use a special method on Element called getMixedContent(). It returns a List of content that may contain instances of Comment, String, Element, and ProcessingInstruction. Java programmers can use instanceof to determine what's what and act accordingly. That code prints out a summary of an element's content:
List mixedContent = table.getMixedContent();
Iterator i = mixedContent.iterator();
while (i.hasNext()) {
Object o = i.next();
if (o instanceof Comment) {
// Comment has a toString()
out.println("Comment: " + o);
}
else if (o instanceof String) {
out.println("String: " + o);
}
else if (o instanceof ProcessingInstruction) {
out.println("PI: " + ((ProcessingInstriction)o).getTarget());
}
else if (o instanceof Element) {
out.println("Element: " + ((Element)o).getName());
}
}


Dealing with processing instructions

Processing instructions (often called PIs for short) are something that certain XML documents have in order to control the tool that's processing them. For example, with the Cocoon Web content creation library, the XML files may have cocoon processing instructions that look like this:
<?cocoon-process type="xslt"?>


Each ProcessingInstruction instance has a target and data. The target is the first word, the data is everything afterward, and they're retrieved by using getTarget() and getData().
String target = pi.getTarget(); // cocoon-process
String data = pi.getData(); // type="xslt"


Since the data often appears like a list of attributes, the ProcessingInstruction class internally parses the data and supports getting data attribute values directly with getValue(String name):
String type = pi.getValue("type");  // xslt


You can find PIs anywhere in the document, just like Comment objects, and can retrieve them the same way as Comments -- using getMixedContent():
List mixed = element.getMixedContent();  // List may contain PIs


PIs may reside outside the root Element, in which case they're available using the getMixedContent() method on Document:
List mixed = doc.getMixedContent();


It's actually very common for PIs to be placed outside the root element, so for convenience, the Document class has several methods that help retrieve all the Document-level PIs, either by name or as one large bunch:
List allOfThem = doc.getProcessingInstructions();
List someOfThem = doc.getProcessingInstructions("cocoon-process");
ProcessingInstruction oneOfThem =
doc.getProcessingInstruction("cocoon-process");


That allows the Cocoon parser to read the first cocoon-process type with code like this:
String type =
doc.getProcessingInstruction("cocoon-process").getValue("type");


As you probably expect, getProcessingInstruction(String) will return null if no such PI exists.

Namespaces

Namespaces are an advanced XML concept that has been gaining in importance. Namespaces allow elements with the same local name to be treated differently because they're in different namespaces. It works similarly to Java packages and helps avoid name collisions.
Namespaces are supported in JDOM using the helper class org.jdom.Namespace. You retrieve namespaces using the Namespace.getNamespace(String prefix, String uri) method. In XML the following code declares the xhtml prefix to correspond to the URL "http://www.w3.org/1999/xhtml". Then <xhtml:title> is treated as a title in the "http://www.w3.org/1999/xhtml" namespace.
<html xmlns:xhtml="http://www.w3.org/1999/xhtml">


When a child is in a namespace, you can retrieve it using overloaded versions of getChild() and getChildren() that take a second Namespace argument.
Namespace ns =
Namespace.getNamespace("xhtml", "http://www.w3.org/1999/xhtml");
List kids = element.getChildren("p", ns);
Element kid = element.getChild("title", ns);


If a Namespace is not given, the element is assumed to be in the default namespace, which lets Java programmers ignore namespaces if they so desire.

Making a list, checking it twice

JDOM has been designed using the List and Map interfaces from the Java 2 Collections API. The Collections API provides JDOM with great power and flexibility through standard APIs. It does mean that to use JDOM, you either have to use Java 2 (JDK 1.2) or use JDK 1.1 with the Collections library installed.
All the List and Map objects are mutable, meaning their contents can be changed, reordered, added to, or deleted, and the change will affect the Document itself -- unless you explicitly copy the List or Map first. We'll get deeper into that in Part 2 of the article.

Exceptions

As you probably noticed, several exception classes in the JDOM library can be thrown to indicate various error situations. As a convenience, all of those exceptions extend the same base class, JDOMException. That allows you the flexibility to catch specific exceptions or all JDOM exceptions with a single try/catch block. JDOMException itself is usually thrown to indicate the occurrence of an underlying exception such as a parse error; in that case, you can retrieve the root cause exception using the getRootCause() method. That is similar to how RemoteException behaves in RMI code and how ServletException behaves in servlet code. However, the underlying exception isn't often needed because the JDOMException message contains information such as the parse problem and line number.

Reading the DocType via jdom

The DocType looks like this in xml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

The first word after DOCTYPE indicates the name of the element being constrained, the word after PUBLIC is the document type's public identifier, and the last word is the document type's system identifier. The DocType is available by calling getDocType() on a Document, and the DocType class has methods to get the individual pieces of the DOCTYPE declaration.

DocType docType = doc.getDocType();
System.out.println("Element: " + docType.getElementName());
System.out.println("Public ID: " + docType.getPublicID());
System.out.println("System ID: " + docType.getSystemID());