Friday, 29 February 2008

Rss server generate whole rss feed from separate rss files

import javax.xml.parsers.*;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

import java.io.File;

/*
* This is a java program to accumulate all
* xml files in some specific directory into
* one xml file, in this case it's much easier
* to maintain the xml server file system and
* provide costumers the xml file they need.
*
* Author: Cross
*/

public class domjaxp {

public static void main(String[] args) {

try {
// Jaxp: create a parser/builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();

// Create the new xml file to accumulate all separate xml file into this one.
Document newdoc = parser.newDocument();

// As rss, Create the root tag, which is <rss>......</rss>
Element newrss = newdoc.createElement("rss");
newrss.setAttribute("version", "2.0");
newdoc.appendChild(newrss);

// As rss, under the root (rss), you must have a channel as a news provider
Element newchannel = newdoc.createElement("channel");
newrss.appendChild(newchannel);

// Basic information of the channel.
Element newtitle = newdoc.createElement("title");
newtitle.setTextContent("ANP unit");
Element newlink = newdoc.createElement("link");
newlink.setTextContent("http://blackboard.lsbu.ac.uk/webapps/portal/frameset.jsp?tab=courses&url=/bin/common/course.pl?course_id=_52692_1");
Element newdescription = newdoc.createElement("description");
newdescription.setTextContent("A rss feed example for ANP unit");

newchannel.appendChild(newtitle);
newchannel.appendChild(newlink);
newchannel.appendChild(newdescription);

// Goto the folder to retrive all the separate rss/xml files.
String path = "src"; // change to your path, here src is used in Eclipse.
File dir = new File(path);
String[] mylist=dir.list(); // Scan the path find the files in it.

// Start to parse each rss/xml file.
for (int i=0;i<mylist.length;i++)
{
String filename = "src\\"+mylist[i];
if (filename.endsWith(".rss")){
System.out.println(filename);

try {
// parse the rss file.
Document d = parser.parse(new File(filename));

// Because we have known the tag name we need, so go for it directly.
NodeList itemlevellist = d.getElementsByTagName("item");

// Get the element of item, which is the story in the rss.
Element itemelement = (Element) itemlevellist.item(0);

// Attach this item into the new xml Document. P.s.: you must import the node.
Node importednode = newdoc.importNode(itemelement, true);
newchannel.appendChild(importednode);

} catch (Exception e) {
e.printStackTrace();
}

} // end if
} // end for

// output the xml document to a new xml file.
try {
File file = new File("newxml.rss");

// DOM dcoument can not be outpur directly, you should make it as a kind of source.
Source source = new DOMSource(newdoc);

// Create file, you must have a kind of stream.
Result result = new StreamResult(file);

// Transformer can make the DOM document source to a file.
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);

} catch (Exception e) {
e.printStackTrace();
}
} // end try
catch (Exception e) {
e.printStackTrace();
}
} // end main
}

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter