怎么用JAVA做个RSS阅读器 求代码
3个回答
2011-12-16
展开全部
解析XML 希望对你有帮助
public class ParseXML {
//下载一个XML
public void downloadXMLFile(String url,String dir) throws IOException{
//下载的文件夹创建
File ff = new File(dir);
if(!ff.exists()){
ff.mkdir();
}
//爬取指定url下的内容
URL u = new URL(url);
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//d:xml
FileWriter fw = new FileWriter(dir+File.separator+getFileNameByURL(url));
BufferedWriter bw = new BufferedWriter(fw);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
is.close();
fw.close();
}
//解析xml
public List<News> parseXML(File file) throws DocumentException{
//创建解析器
SAXReader sr = new SAXReader();
//要解析的文件
Document doc = sr.read(file);
//获得跟节点
Element e = doc.getRootElement();
System.out.println(e.getName());
List<News> list = new ArrayList<News>();
//从跟节点下查找某节点
List<Element> listTitle = e.selectNodes(Common.title);
List<Element> listLink = e.selectNodes(Common.link);
List<Element> listDesc = e.selectNodes(Common.desc);
List<Element> listPub = e.selectNodes(Common.pubDate);
for(int i=0;i<listTitle.size();i++){
News news = new News();
news.setNTITLE(listTitle.get(i).getText());
news.setNLINK(listLink.get(i).getText());
news.setNDESC(listDesc.get(i).getText());
news.setNPUBDATE(listPub.get(i).getText());
System.out.println(listTitle.get(i).getText());
System.out.println(listLink.get(i).getText());
list.add(news);
}
return list;
}
//获取文件名
public String getFileNameByURL(String url){
String[] names = url.split("/");
return names[names.length-1];
}
public static void main(String[] args){
ParseXML px = new ParseXML();
try {
px.downloadXMLFile("http://news.163.com/special/00011K6L/rss_newstop.xml", "f://xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f = new File("f://xml//rss_newstop.xml");//XML
try {
List<News> list = px.parseXML(f);
NewsServiceImple nsi = new NewsServiceImple();
nsi.insertNews(list, f.getName());
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
public class ParseXML {
//下载一个XML
public void downloadXMLFile(String url,String dir) throws IOException{
//下载的文件夹创建
File ff = new File(dir);
if(!ff.exists()){
ff.mkdir();
}
//爬取指定url下的内容
URL u = new URL(url);
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//d:xml
FileWriter fw = new FileWriter(dir+File.separator+getFileNameByURL(url));
BufferedWriter bw = new BufferedWriter(fw);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
is.close();
fw.close();
}
//解析xml
public List<News> parseXML(File file) throws DocumentException{
//创建解析器
SAXReader sr = new SAXReader();
//要解析的文件
Document doc = sr.read(file);
//获得跟节点
Element e = doc.getRootElement();
System.out.println(e.getName());
List<News> list = new ArrayList<News>();
//从跟节点下查找某节点
List<Element> listTitle = e.selectNodes(Common.title);
List<Element> listLink = e.selectNodes(Common.link);
List<Element> listDesc = e.selectNodes(Common.desc);
List<Element> listPub = e.selectNodes(Common.pubDate);
for(int i=0;i<listTitle.size();i++){
News news = new News();
news.setNTITLE(listTitle.get(i).getText());
news.setNLINK(listLink.get(i).getText());
news.setNDESC(listDesc.get(i).getText());
news.setNPUBDATE(listPub.get(i).getText());
System.out.println(listTitle.get(i).getText());
System.out.println(listLink.get(i).getText());
list.add(news);
}
return list;
}
//获取文件名
public String getFileNameByURL(String url){
String[] names = url.split("/");
return names[names.length-1];
}
public static void main(String[] args){
ParseXML px = new ParseXML();
try {
px.downloadXMLFile("http://news.163.com/special/00011K6L/rss_newstop.xml", "f://xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f = new File("f://xml//rss_newstop.xml");//XML
try {
List<News> list = px.parseXML(f);
NewsServiceImple nsi = new NewsServiceImple();
nsi.insertNews(list, f.getName());
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
展开全部
RSS没有设么难的,其实就是暴露在公网上的XML。读取公网XML地址,然后用JAVA解析XML即可!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我用java做过手机端的RSS阅读器。首先要有RSS网址,然后需要一个解析XML的类,XML的解析也可以自己来写,就是麻烦些。其他的没什么大问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询