Posts Tagged ‘java’
http://javamix.net
Dear blog readers,
For providing some better options to users i moved this JavaMix blog from wordpress to it`s own domain
you can find countinious updates here after afew days.
follow this blog updates at twitter http://twitter.com/javamixblog
thanks,
Katta Vijay
Read RSS feeds using JSP
<%@page contentType=”text/html”%>
<%@page pageEncoding=”UTF-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<%@page import=”java.net.URL”%>
<%@page import=”javax.xml.parsers.DocumentBuilder”%>
<%@page import=”javax.xml.parsers.DocumentBuilderFactory”%>
<%@page import=” org.w3c.dom.CharacterData”%>
<%@page import=” org.w3c.dom.Document”%>
<%@page import=”org.w3c.dom.Element”%>
<%@page import=”org.w3c.dom.Node”%>
<%@page import=”org.w3c.dom.NodeList”%>
<%@page import=”java.lang.*”%>
<%
String desc=null;
try
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL(“http://feed43.com/eenadu.xml”); // feed address
Document doc = builder.parse(u.openStream());
String title;
NodeList nodes = doc.getElementsByTagName(“item”);
for(int i=0;i<nodes.getLength();i++) {
Element element = (Element)nodes.item(i);
out.println(“Title: ” + getElementValue(element,”title”));
out.println(“Link: ” + getElementValue(element,”link”));
out.println(“Publish Date: ” + getElementValue(element,”pubDate”));
out.println(“author: ” + getElementValue(element,”dc:creator”));
out.println(“comments: ” + getElementValue(element,”wfw:comment”));
desc=getElementValue(element,”description”);
out.println(“description: ” + desc);
out.println();
out.println(” tested”);
}//for
}//try
catch(Exception ex) {
ex.printStackTrace();
}
%>
<%!
public String getElementValue(Element parent,String label) {
return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
}
public String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if(child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
}
catch(Exception ex) {
}
return ” “;
} //private String getCharacterDataFromElement
%>
Read RSS feeds by Using Java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author vijay
*
*/
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class RSSReader {
private static RSSReader instance = null;
private RSSReader() {
}
public static RSSReader getInstance() {
if(instance == null) {
instance = new RSSReader();
}
return instance;
}
public void writeNews() {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL(“http://pratyush.in/feed/rss”); // your feed url
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName(“item”);
for(int i=0;i<nodes.getLength();i++) {
Element element = (Element)nodes.item(i);
System.out.println(“Title: ” + getElementValue(element,”title”));
System.out.println(“Link: ” + getElementValue(element,”link”));
System.out.println(“Publish Date: ” + getElementValue(element,”pubDate”));
System.out.println(“author: ” + getElementValue(element,”dc:creator”));
System.out.println(“comments: ” + getElementValue(element,”wfw:comment”));
System.out.println(“description: ” + getElementValue(element,”description”));
System.out.println();
}//for
}//try
catch(Exception ex) {
ex.printStackTrace();
}
}
private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if(child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
}
catch(Exception ex) {
}
return “”;
} //private String getCharacterDataFromElement
protected float getFloat(String value) {
if(value != null && !value.equals(“”)) {
return Float.parseFloat(value);
}
return 0;
}
protected String getElementValue(Element parent,String label) {
return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
}
public static void main(String[] args) {
RSSReader reader = RSSReader.getInstance();
reader.writeNews();
}
}
Note: this is not my program, i copied from java forums, after success full running i feel that it may help for my blog reader. i don`t have any rights on this program.
you can find original post @ http://forums.sun.com/thread.jspa?threadID=5275485
reading data from the .Doc file by using Apache POI api
this program simply explains how to read data from the MS wordfile(.DOC) line by line using Apache POI,
what is Apache POI and what is the need i already explain in previous post, you can find that post here
for executing this program we need to download Apache POI api and make jar files in classpath.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package multidocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
/**
*
* @author vijay
*/
public class NewDocReader {
public static void main(String args[]) throws FileNotFoundException, IOException
{
File docFile=new File(“c:\\multi\\multi.doc”); // file object was created
FileInputStream finStream=new FileInputStream(docFile.getAbsolutePath()); // file input stream with docFile
HWPFDocument doc=new HWPFDocument(finStream);// throws IOException and need to import org.apache.poi.hwpf.HWPFDocument;
WordExtractor wordExtract=new WordExtractor(doc); // import org.apache.poi.hwpf.extractor.WordExtractor
String [] dataArray =wordExtract.getParagraphText();
// dataArray stores the each line from the document
for(int i=0;i<dataArray.length;i++)
{
System.out.println(“\n–“+dataArray[i]);
// printing lines from the array
}
finStream.close(); //closing fileinputstream
}
}