java读取rss

<转载于http://zfive.blogbus.com/logs/20959998.html>

package com.yourcompany.struts.user;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.net.URL;

public class Test {
 public static void main(String[] args) {
  try{
   List result = new ArrayList();  //声明一个结果集
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //解析器工厂类
   DocumentBuilder db;
   Document doc;
  
   //此DocumentBuilder对象的作用是根据xml文件的url地址生成document对象
   db =dbf.newDocumentBuilder();     //得到一个DOM解析器对象;
    //File file = new File("students.xml"); //当然,我们也可以读出本地计算机中的xml文档
  
   //doc对象包含需要解析的feed的xml文件
   URL file = new URL("http://localhost:8080/rSS/pfocus.xml");
   
   doc = db.parse(file.openStream());  //把解析后的XML,赋给文档对象; 
   //feed的xml文件的组成单位是item,从doc中取出所有的item
   NodeList nl = doc.getElementsByTagName("item");  //多个<item>标签组成一个链表;
 
   for(int i=0;i<nl.getLength();i++)
   {
    Element eltStu = (Element)nl.item(i);

    //此处得到元素中的<title></title>标签实体;
    Node titlenode = eltStu.getElementsByTagName("title").item(0);  

    //返回第一个节点的值;

      String title = titlenode.getFirstChild().getNodeValue();
    
    Node linknode = eltStu.getElementsByTagName("link").item(0);
    String link = linknode.getFirstChild().getNodeValue();
    
    Node descriptionnode = eltStu.getElementsByTagName("description").item(0);
    String description = descriptionnode.getFirstChild().getNodeValue();
    
    Node authornode = eltStu.getElementsByTagName("author").item(0);
    String author = authornode.getFirstChild().getNodeValue();
    
    
    System.out.println(title);
    System.out.println(author);
    System.out.println(link);
    System.out.println(description);
    System.out.println("---------");
      }
  }
   catch(ParserConfigurationException e)
   {
    e.printStackTrace();
   }catch(SAXException e){
    e.printStackTrace();
   }catch(IOException e){
    e.printStackTrace();
   }finally{
   }

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值