利用Struts开发Rss在线阅读器

来源:互联网 发布:js获取body所有子元素 编辑:程序博客网 时间:2024/05/17 10:09

RSS文件是一种XML文件

<?xml version="1.0" encoding="GB18030"?>

<rss version="2.0">

<channel>

<title>W3Schools Home Page</title>

<link>http://www.W3Schools.com</link>

<description>Free web building tutorials</description>

<item>

  <title>RSS Tutorial</title>

  <link>http://www.w3schools.com/rss</link>

  <description>New RSS tutorial on W3Schools</description>

</item>

<item>

  <title>XML Tutorial</title>

  <link>http://www.w3schools.com/xml</link>

  <description>New xml tutorial on W3Schools</description>

</item>

</channel>

</rss>

 

解析代码:

public static List parseFeed(String address){//通过address地址来解析XML
        //result 包含从feed中得到的一组Item对
        List result = new ArrayList() ;
        //定义从DocumentBuilderFactory,DocumentBuilder 和Documen, 用以解析代表rss feed的xml文件
        //通过DocumentBuilderFactory.newInstance()方法得到DocumentBuilderFactory的对象
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance() ;
        DocumentBuilder db ;
        Document doc ;
        try{
            //利用DocumentBuilderFactory对象的newDocumentBuilder方法得到DocumentBuilder对象
            //此DocumentBuilder对象的作用是根据xml文件的url地址生成Document对象
            db = dbf.newDocumentBuilder() ;
            //doc对象包含需要解析的feed的xml文件
            doc = db.parse(address) ;
            //feed的xml文件的组成单位是item ,从doc中取出所有的item
            NodeList nl = doc.getElementsByTagName("item") ;
            //遍历这些item, 取得其中的title, author, Description 和url等信息
            for(int i = 0, j= nl.getLength(); i<j; i++){
                Node node = nl.item(i) ;
                NodeList nl2 = node.getChildNodes() ;
               
                Node nodeTitle = nl2.item(1) ;
                Node nodeAuthor = nl2.item(2) ;
                Node nodeUrl = nl2.item(3) ;
                Node nodeDescription = nl2.item(4) ;
                String title = nodeTitle.getFirstChild().getNodeValue() ;
                String description = nodeDescription.getFirstChild().getNodeValue() ;
                String author = nodeAuthor.getFirstChild().getNodeValue() ;
                String url = nodeUrl.getFirstChild().getNodeValue()  ;
                //通过得到的title、author、Description 和 url来构造item对象
                Item item = new Item(title, description, author, url) ;
                //将生成的item放到result中,以便返回
                result.add(item) ;
            }
        }catch(ParserConfigurationException e){
            //处理ParserCOnfigurationException异常,输出异常信息
            e.printStackTrace() ;
        }catch(SAXException e){
            //处理SAXException异常,输出异常信息
            e.printStackTrace() ;
        }catch(IOException e){
            //处理IOException异常,输出异常信息
            e.printStackTrace() ;
        }finally{
            //将结果返回, result数组可能为空
            return result ;
        }
       
    }

 

显示jsp页面:

<!-- 若没有得到文章,则显示提示信息, 使用logic:empty标签来实现此功能 -->

<logic:empty name="rssReaderForm" property="items">
        <center>此feed内目前没有包含文章</center>
</logic:empty>
    <!-- 若文章列表不为空,则循环显示每篇文章的题目、作者、链接和内容 -->
    <logic:notEmpty name="rssReaderForm" property="items">
        <center>文章列表:</center>    <br>
        <!-- 使用logic:iterate标签循环显示文章,设置id并通过它来调用item中的内容-->
        <logic:iterate indexId="index" id="item" name="rssReaderForm" property="items">
            <table>
                <tr><!-- 分别得到item的title、url和author信息 -->
                    <td>
                        <strong>标题:</strong>
                        <a href="<bean:write name="item" property="url"/>">
                            <bean:write name="item" property="title"/>
                        </a>
                        (<strong>by:</strong><bean:write name="item" property="author"/>)
                        <br>
                    </td>
                </tr>
                <tr><!--显示文章内容  -->
                    <td>
                        <bean:write name="item" property="description"/>
                        <br>
                    </td>
                </tr>
            </table>
        </logic:iterate>
    </logic:notEmpty>

 

原创粉丝点击