Updates from February, 2009 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 6:10 pm on February 4, 2009 Permalink |
    Tags: , jdom, , , , ,   

    A basic Jdom parser for RSS 

    128px-feed-iconsvg

    Almost two years back I posted a SAX based RSS parser (find it here) which was intented for J2ME. But we have JDOM parser which I think is a lot easier than SAX. In this post you can find a very simple JDOM based RSS parser.

    Know more about RSS here: http://en.wikipedia.org/wiki/RSS_(file_format)

    Step1: Import the JDOM libraries

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    


    Step 2:
    Initialize

    // I have not given the implementation of
    // getUrlConnectionInputStream(url)
    inputStream = getUrlConnectionInputStream(url);
    if(null == inputStream)
       throw new Exception("No input stream for " + url);
    
    saxBuilder = new SAXBuilder();
    document = saxBuilder.build(inputStream);
    rssFeed = _build(document);
    


    Step 3:
    Implementation of _build(org.jdom.Document)

    // Entry point. Returns a RssFeed object corresponding
    // to the given RSS feed URL
    private RssFeed _build(Document document)
    throws Exception
    {
      RssFeed rssFeed = null;
      Element rootElement = null;
      Element channelElement = null;
      String  rssFeedVersion = null;
    
      if (null == document)
        throw new Exception("Empty document");
    
      rootElement = document.getRootElement();
      if (!"rss".equalsIgnoreCase(rootElement.getName()))
        throw new Exception("Invalid XML");
    
      rssFeedVersion = rootElement.getAttributeValue("version");
      channelElement = rootElement.getChild("channel");
      if (null == channelElement)
        throw new Exception("Empty feed");
    
      // Getting the feed contents
      rssFeed = _getHeader(channelElement);
      rssFeed.version = rssFeedVersion;
      _addFeedItems(channelElement, rssFeed);
    
      return (rssFeed);
    }
    

    Step 4: _getHeader(org.jdom.Element)

    This method reads the feed header and sets the values to the RssFeed object.

    // Sets the RSS feed heder information to the
    // RssFeed object
    rssFeed = new RssFeed();
    rssFeed.title = getValueOfChildElement(channelElement, "title");
    rssFeed.link = getValueOfChildElement(channelElement, "link");
    rssFeed.description = getValueOfChildElement(channelElement, "description");
    


    Step 5:
    _addFeedItems(org.jdom.Elements,  subin.xml.RssFeed)

    This method extracts each feed item from the XML and adds it to the given RssFeed object.

    // Iterates through the feed item list, extracts the
    // feed item details, creates corresponding RssItem object
    // and adds it to the RssFeed item list
    java.util.List<element> itemElements = null;
    RssItem anRssItem = null;
    
    itemElements = channelElement.getChildren("item");
    if (null != itemElements)
    {
      for (Element anItemElement : itemElements)
      {
        anRssItem = new RssItem();
        anRssItem.title = getValueOfChildElement(anItemElement, "title");
        anRssItem.link = getValueOfChildElement(anItemElement, "link");
        anRssItem.description = getValueOfChildElement(anItemElement, "description");
        anRssItem.pubDate = getValueOfChildElement(anItemElement, "pubDate");
        rssFeed.addItem(anRssItem);
      }
    }
    

    Step 6: getValueOfChildElement(org.jdom.Element, String)

    This method extract the value of the child node (specified by the name) from the given JDOM Element.

    // Get the child node value
    private String getValueOfChildElement(Element parentElement,
      String tagName)
    {
      Element childElement = null;
      String  tagValue     = null;
      childElement = parentElement.getChild(tagName);
      tagValue = (null != childElement)
        ? childElement.getValue().trim() : null;
      return (tagValue);
    }
    

    Step 7: RssFeed & RssItem

    Two classes to hold the feed information.

    class RssFeed
    {
      public  String version;
      public  String title;
      public  String description;
      public  String link;
    
      public List <rssitem> items;
    }
    
    class RssItem
    {
      public  String title;
      public  String description;
      public  String link;
      public  String pubDate;
    }
    

    If you find it difficult to follow as it is not a single file, I’m very sorry. But I hope this will be useful.

     
    • jimmyzhang 7:23 am on December 2, 2009 Permalink

      you may also want to check out vtd-xml, the latest and most advanced xml processing model

      vtd-xml

    • Subinkrishna G 2:45 pm on December 2, 2009 Permalink

      Jimmy, Thanks for the information. I will definitely have a look at it.

      Subin

  • Subinkrishna Gopi 4:42 pm on June 2, 2008 Permalink |  

    Collection & null 

    Can a Hashtable have a “null” key? Can a TreeSet hold a null? Confused?
    Check the following table to know whether different data structures support null or not.

    Collection       Key      Value
    HashMap          Yes      Yes
    HashTable        No       No
    TreeMap          No       Yes
    
    LinkedList       -        Yes
    ArrayList        -        Yes
    Vector           -        Yes
    
    HashSet          -        Yes
    TreeSet          -        No


    Note:

    TreeMap & TreeSet are sorted data structures. Thats why they don’t support null in their sort field.

     
  • Subinkrishna Gopi 2:57 pm on September 14, 2007 Permalink |  

    Java performance tips – Part 2 : Better String handling 

    StringBuffer vs String

    As we all know both String & StringBuffer are used to hold string (character sequence) values. The basic & the most important difference between the objects being created using String & StringBuffer : objects of String are immutable (means, no one can change it); but the objects created using StringBuffer are mutable. Consider the following code block,

    // Example 1 
    String aString = "Hello"; 
    aString = aString + "World !";
    // Example 2 
    StringBuffer aStringBuffer = new StringBuffer("Hello"); 
    aStringBuffer.append("World !");

    Here both example 1 & 2 are doing the same thing. If someone ask us which one we think is a better way to append a string to other, hmmm… sometimes, no most of the times people may choose the first method. B’ coz the “+” operator seems super cool, right?

    The String concatenation using the “+” works in a crazy way. It will internally create a temporary StringBuffer object using the current value and will use the append() to get the things done. Why? Any guess? Yeah, you are right. B’ coz the object created using String is immutable.

    I found the byte-code equivalent of both the string concatenation from javaworld.com. Let’s have a look at it.

    Byte-code equivalent of example 1:

    0 new #7 <Class java.lang.String> 
    3 dup 
    4 ldc #2 <String "Hello"> 
    6 invokespecial #12 <Method java.lang.String(java.lang.String)> 
    9 astore_1 
    10 new #8 <Class java.lang.StringBuffer> 
    13 dup 
    14 aload_1 
    15 invokestatic #23 <Method java.lang.String valueOf(java.lang.Object)> 
    18 invokespecial #13 <Method java.lang.StringBuffer(java.lang.String)> 
    21 ldc #1 <String "World !"> 
    23 invokevirtual #15 <Method java.lang.StringBuffer append(java.lang.String)> 
    26 invokevirtual #22 <Method java.lang.String toString()> 
    29 astore_1

    Byte-code equivalent of example 2:

    0 new #8 <Class java.lang.StringBuffer> 
    3 dup 
    4 ldc #2 <String "Hello"> 
    6 invokespecial #13 <Method java.lang.StringBuffer(java.lang.String)> 
    9 astore_1 
    10 aload_1 
    11 ldc #1 <String "World !"> 
    13 invokevirtual #15 <Method java.lang.StringBuffer append(java.lang.String)> 
    16 pop

    I donno how to interpret it. But we can easily understand what is happening there, right?

    StringBuffer vs StringBuilder

    StringBuilder is same as StringBuffer but not thread-safe. All the public methods of StringBuffer are synchronized. StringBuilder offers better performance than StringBuffer in most of the conditions. StringBuffer is advised to use only if some kind of synchronization is needs.

     
  • Subinkrishna Gopi 3:29 pm on September 13, 2007 Permalink |  

    Java performance tips – Part 1 : Vector, ArrayList & LinkedList 

    When, where & how to use Vector & ArrayList

    Even though both Vector & ArrayList are used for the same purpose – as a dynamic collection of related objects – there is a right time and place to use them. As we all know there is a major difference: Vector is thread-safe but ArrayList is not. If there is no need of synchronization we can use ArrayList instead of Vector. And that will definitely be a performance booster.

    When size matters

    Internally both Vector & ArrayList uses arrays to hold the elements. Both supports an initialCapacity parameter which decides the initial size of the array. (I really don’t remember the default value). Once if the capacity becomes insufficient to accommodate new elements either of these collections need to expand their capacity. Both Vector & ArrayList do it differently. Vector doubles the current capacity & ArrayList increases the capacity by 50%. Whenever we create a Vector we can specify a capacityIncrement parameter; which is an integer using which the capacity will get incremented whenever the Vector overflows.

    Its also advised that, if we know the maximum number of elements that gonna be inserted in to the collection, we should set it as the initialCapacity while creating the collection itself. But its not practically possible every time.

    Element access & performance

    In both Vector & ArrayList addition/deletion of elements at one end & accessing the elements at a particular index wont take much effort (just O(1) ). But adding or deleting an element at the i-th position will take some effort based on the value of i (O(n-i), where n is the size of the collection). Use of a LinkedList can help us here. B’ coz in a LinkedList addition/deletion of elements is pretty easy as it’s a matter of adjusting the pointers (just O(1)).

    LinkedList is good, but…

    The problem with LinkedList is that it will create an object for each element being added to the list. Means double the duty for the garbage collector. And as LinkedList is not using arrays to hold elements, it’s a bit painful for it to access elements at a particular index (O(i), where i the index).

    So, programmers should decide when to use what based on the requirement.

     
    • yogitag 5:23 am on February 25, 2012 Permalink

      Good Information.. Thanks

    • javarevisited 9:58 am on March 30, 2012 Permalink

      LinkedList is good for insertion and deletion from beginning and end. Though Finding the element in the middle of the LinkedList takes too much time as the benefits of just changing the pointer are lost. So, LinkedList is worse than ArrayList for removing elements in the middle see here for some difference between LinkedList and ArrayList in Java

  • Subinkrishna Gopi 3:15 pm on July 13, 2007 Permalink |
    Tags: collections, , map, ,   

    TreeMap vs HashMap 

    Both TreeMap & HashMap are two different implementation of the Map interface. Even though this post is titled “TreeMap vs HashMap” I would like to say how they are connected and how much similar they are.

    Both TreeMap & HashMap are not synchronized. To make it synchronized we have to explicitly call Collections.synchronizedMap( mapName ) . Both supports “fail-fast” iterators. Both of them doesn’t support duplicate keys.

    HashMap

    HashMap allows null as both keys and values. HashMap is useful when we need to access the map without cosidering how they are added to the map (means, unordered lookup of values using their keys). HashMap is synchronized while it is being looked up. HashMap doesn’t allow duplicated entries.

    The performance of HashMap is based on two optional parameter which we can specify during the creation of the HashMap. Initial capacity & load factor. Initial capacity is the bucket size assigned to a HashMap during it’s creation. Load factor decides when the HashMap needs to be expanded. If the load factor is 0.75, the size will be increased when the current size of the map crosses 75% of its capacity.

    TreeMap

    The basic difference between HashMap & TreeMap is that, in a TreeMap the elements are stored in a tree. TreeMap allows us to retrieve the elements in some sorted order defined by the user. So we can say that TreeMap is slower than HashMap. This is the only implementation based on SortedMap interface.

    TreeMap allows us to specify an optional Comparator object during its creation. The keys should be compatible with the comparator specified. This comparator decides the order by which the keys need to be sorted.

    public interface Comparator
    {
        public int compare (Object object1, Object object2);
        public boolean equals (Object object);
    }
    

    If we are not specifying the comparator, TreeMap will try to sort the keys in the natural order. Means will consider them as instances of Comparable interface.

    public interface Comparable
    {
        public int compareTo (Object objectToCompare);
    }
    

    Classes like Integer, String, Double etc implements Comparable interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the Comparable interface.

    public class MyCustomKey implements Comparable
    {
        private int value;
        public MyCustomKey(int value)
        {
           this.value = value;
        }           
    
        public int compareTo (MyCustomKey key)
        {
           int comparison = 0;           
    
           // Note:
           // Return -1 if this.value < key.value
           // Return  0 if this.value = key.value
           // Return  1 if this.value > key.value           
    
           return (comparison);
        }
    }
    

    A common mistake that everyone does is not to override the hashcode(). If we are failing to do so, map.get(new MyCustomKey(<value>)); may not give you what you were expecting. So it is always adviced to override the hashCode() if objects of that class is being used as a key.

    public class MyCustomKey implements Comparable
    {
        private int value;
        public MyCustomKey(int value)
        {}
        public int compareTo (MyCustomKey key)
        {}        
    
        public int hashCode()
        {
            // Note:
            // If two objects are equal then their corresponding hashCode ()
            // should return the same value too.
            return (this.value * 199);
        }
    }
    
     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel