|
|||||||||||||||
|
RSS Reader Tutorial XTOM Basics First lets introduce the main components of XTOM Parser. There are only three class that you as a programmer will have to work with. These are :
Parser - this class is responsible for parsing the XML input stream or string into an XMLTree. XMLTree - is responsible for containing the reference to the root element. It does not do much right now except for holding the referecen, but this might change in the future. Element - this is the most important class of XTOM. An Element represents a Node in an XML Document. It does all of the work behind the navigation of the tree of elements. A programmer will do most of his/her work using objects of this class. RSS Reader Implementation This example RSS Reader consists of four files. Three of these files, mainly Feed, Channel, and Item are just data holders. RSSReader performs all of the parsing and data retreival work. We will be examining RSSReader in this tutorial. For future reference review the structure of the Feed, Channel and Item before proceeding further. The RSSReader class can be found here. The RSSReader has two parts. First part is reading the RSS Feed from the web and second one is parsing the feed. Reading the RSS Feed is simple:
The above segment of code opens a URL connection and reads the data into a 'xml' String instance which is a class level variable. Next the RSSReader implements the parsing of the Feed in the parse() method:
The last statement calls the parseFeed method passing the reference of the root element. The iimportant thing to keep in mind here is that the XMLTree only holds the reference to the Root Element of the XML Tree. Next we will be navigating the XML Element Tree using methods 'getElementByPath' and 'getElementsByPath' and populating the Feed, Channel and Item instances. This is done in the parseFeed(rootElement) method:
First a new instance of Feeder is created. Then using the getAttribute(name) method of the Element class we try to extract the version attribute from the root Element. If the attribute is not present then the attribute is ignored. Next we get the Element corresponding to the 'channel' node of the RSS feed by calling the 'getElementByPath('path/to/element') method of the root Element. In this case we only need to specify the name 'channel'. Remember: The path is always relative to the element you are calling getElementByPath method from. Once we have the Element for the 'channel' node we can start filling up the Channel and Item iinstances for the feed. First we create the Channel class instance and give it a title by extracting the title from the 'title' node of the 'channel' node. We do this by calling the getElementByPath('channel/title') on the root element. The path specified is relative to the root element. Next we extract the data for the Channel instance:
Here the extraction of the data for the Channel instnace is done through the Element representing the 'channel' node in the XML. For each property of the Channel class an Element representing that property is extracted through the channel element and its value stored in the corresponding property. The second last thing that needs to be done is the extraction of Item nodes:
Here we retreive the list of Elements corresponding to the list of 'item' nodes in the RSS feed by calling a 'getElementsByPath('item') on the channel Element instance. This returns an array of Elements representing the 'items'. Then we go through each element and extract the information, which is then stored as properties of an Item object. One thing to notice here the is use of getValueAsBoolean method. An Elelement class can extract the data and parse it as any of the primitive types or a String. Helper methods are provided by default. Now once the data extraction is complete all that is left to do is add teh List of items to the Channel instance, and the Channel instance to the Feed instance and return the Feed instance.
That's it we're done. Check out more tutorials for advanced topics such how XTOM Exceptions are thrown and when to handle them. |
||||||||||||||