|
-
July 18th, 2008, 06:25 AM
#1
How to display XML File in JSP?
Hi
I have one XML file. Its actualy a RSS file. I wnat to display my RSS in JSP and then when i will open that JSP, my RSS reader which is inbuilt in my Explorer, will display the XML file.
How to acieve it? I think MIME can be used. But not able to find much about it. How cna i do it ?
-
July 18th, 2008, 06:57 AM
#2
Re: How to display XML File in JSP?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
July 18th, 2008, 06:24 PM
#3
Re: How to display XML File in JSP?
This is one of many manual ways to do it (sry for php tag, but the code looks more readable this way):
PHP Code:
import java.net.URL;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class test {
public static void main(String[] args){
ArrayList<String[]> rssItems = getRSSitems();
for(int i=0; i<rssItems.size(); i++){
String[] rssItem = rssItems.get(i);
System.out.println("title: "+rssItem[0]);
System.out.println("description: "+rssItem[1]);
System.out.println("link: "+rssItem[2]);
System.out.println("guid: "+rssItem[3]);
System.out.println("pubDate: "+rssItem[4]);
System.out.println("category: "+rssItem[5]);
System.out.println("IMG attrubutes: width="+rssItem[6]+
", height="+rssItem[7]+", url="+rssItem[8]+"\n");
}
}
private static ArrayList<String[]> getRSSitems(){
ArrayList<String[]> rssItems = new ArrayList<String[]>();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//remote rss xml
URL url = new URL("http://newsrss.bbc.co.uk/rss/sportonline_world_edition/front_page/rss.xml");
Document doc = docBuilder.parse(url.openStream());
/*
//local rss xml
File f = new File("[localPath]/rss.xml");
Document doc = docBuilder.parse(f);
*/
doc.getDocumentElement().normalize();
//list xml by tags (this is not a generic solution
//so we must know exact rss xml structure)
NodeList nlItems = doc.getElementsByTagName("item");
int totalItems = nlItems.getLength();
//System.out.println("Total # of items: " + totalItems);
for(int i=0; i<totalItems; i++){
Node tableNode = nlItems.item(i);
if(tableNode.getNodeType() == Node.ELEMENT_NODE){
String[] rssItem = new String[9];
Element tableElement = (Element)tableNode;
NodeList nodeList = tableElement.getElementsByTagName("title");
Element element = (Element)nodeList.item(0);
NodeList textList = element.getChildNodes();
rssItem[0] = ((Node)textList.item(0)).getNodeValue().trim();
nodeList = tableElement.getElementsByTagName("description");
element = (Element)nodeList.item(0);
textList = element.getChildNodes();
rssItem[1] = ((Node)textList.item(0)).getNodeValue().trim();
nodeList = tableElement.getElementsByTagName("link");
element = (Element)nodeList.item(0);
textList = element.getChildNodes();
rssItem[2] = ((Node)textList.item(0)).getNodeValue().trim();
nodeList = tableElement.getElementsByTagName("guid");
element = (Element)nodeList.item(0);
textList = element.getChildNodes();
rssItem[3] = ((Node)textList.item(0)).getNodeValue().trim();
nodeList = tableElement.getElementsByTagName("pubDate");
element = (Element)nodeList.item(0);
textList = element.getChildNodes();
rssItem[4] = ((Node)textList.item(0)).getNodeValue().trim();
nodeList = tableElement.getElementsByTagName("category");
element = (Element)nodeList.item(0);
textList = element.getChildNodes();
rssItem[5] = ((Node)textList.item(0)).getNodeValue().trim();
nodeList = tableElement.getElementsByTagName("media:thumbnail");
element = (Element)nodeList.item(0);
rssItem[6] = element.getAttribute("width");
rssItem[7] = element.getAttribute("height");
rssItem[8] = element.getAttribute("url");
rssItems.add(rssItem);
}
}
}catch(Exception e){
System.out.println(e.toString());
e.printStackTrace(System.out);
}
return rssItems;
}
}
Forget about heavy trinkets like Sun RSS Utilities, they are buggy, they are too generic.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
July 18th, 2008, 07:07 PM
#4
Re: How to display XML File in JSP?
If I understand the original post correctly then you just want to set the Content-type header to text/xml
Then print out your xml with nothing else.
Hope that helps...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|