KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transactions > rss > RSSConsumer


1 package transactions.rss;
2
3 import dinamica.*;
4 import electric.xml.*;
5
6 /**
7  * Retrieves an XML document (RSS feed) and parse the elements
8  * to produce a Recordset.
9  * <br><br>
10  * Creation date: 10/03/2004<br>
11  * Last Update: 10/03/2004<br>
12  * (c) 2004 Martin Cordova<br>
13  * This code is released under the LGPL license<br>
14  * @author Martin Cordova (dinamica@martincordova.com)
15  * */

16 public class RSSConsumer extends GenericTransaction
17 {
18
19     /* (non-Javadoc)
20      * @see dinamica.GenericTransaction#service(dinamica.Recordset)
21      */

22     public int service(Recordset inputParams) throws Throwable JavaDoc
23     {
24         
25         //reuse superclass code
26
int rc = super.service(inputParams);
27         
28         //define recordset structure (according to RSS standard)
29
Recordset rs = new Recordset();
30         rs.append("title", java.sql.Types.VARCHAR);
31         rs.append("description", java.sql.Types.VARCHAR);
32         rs.append("link", java.sql.Types.VARCHAR);
33         rs.append("pubDate", java.sql.Types.VARCHAR);
34         
35         //search for <url> element in config.xml
36
String JavaDoc url = getConfig().getConfigValue("url");
37         
38         //retrieve XML document via HTTP
39
String JavaDoc data = StringUtil.httpGet(url, true);
40         
41         //parse and navigate XML document
42
Document doc = new Document(data);
43         Element root = doc.getRoot();
44         Elements items = root.getElements(new XPath("//item"));
45         while (items.hasMoreElements())
46         {
47             //add new entry
48
rs.addNew();
49             Element item = items.next();
50             Elements fields = item.getElements();
51             //get item field values
52
while (fields.hasMoreElements())
53             {
54                 Element field = fields.next();
55                 String JavaDoc name = field.getName();
56                 rs.setValue(name, field.getString());
57             }
58         }
59         
60         //publish recordset
61
publish("rss", rs);
62         
63         return rc;
64         
65     }
66
67 }
68
Popular Tags