Rss

hi, plz can anyone tell me how can i make rss code using java
[68 byte] By [Battasa] at [2007-10-3 2:36:38]
# 1

Hi,

you can use velocity template to generate the XML.

http://jakarta.apache.org/velocity/docs/developer-guide.html#How%20Velocity%20Works

An example code is as follows:

***********************************************

1. create a velocity template (Rss.vm)

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:content="http://purl.org/rss/1.0

/modules/content/">

<channel>

<title>$title</title>

<link></link>

<description>$description</description>

<ttl>60</ttl>

<language>en-us</language>

<pubDate>$publishDate</pubDate>

<lastBuildDate>$buildDate</lastBuildDate>

<docs />

<content:encoded>

<![CDATA[

#foreach ( $rssItem in $rssItems)

><item>

<title>$rssItem.title</title>

<link>$rssItem.link</link>

<description>$rssItem.subTitle</description>

<pubDate>$publishDate</pubDate>

</item>

#end

]]>

</content:encoded>

</channel>

</rss>

2. create a servlet which extends VelocityServlet, and

SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");

context.put("publishDate",sdf.format(new Date()));

context.put("buildDate",sdf.format(new Date()));

context.put("rssItems",rssItems);

context.put("title",rssTitle);

context.put("description",rssDescription); //wat ever u want in the template

//Retrieve the Template

template = getTemplate("Rss.vm");

//set the response type to xml

response.setContentType("text/xml");

PrintWriter writer = response.getWriter();

//'Merge' the template and data to produce the ouput.

StringWriter sw = new StringWriter();

template.merge( context, sw );

//write the output to response

writer.write(sw.toString());

***********************

Hope this helps.

VC.

vc_suna at 2007-7-14 19:34:55 > top of Java-index,Other Topics,Fresh Tools...
# 2
Is the Velocity Template obligatory? I mean .. can I just generate the xml using xml java classes and place it in an accessible directory of the web server ?
tlloretia at 2007-7-14 19:34:55 > top of Java-index,Other Topics,Fresh Tools...