利用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>
- 利用Struts开发Rss在线阅读器
- AJAX实践:开发你自己的RSS在线阅读器
- Android开发RSS阅读器
- RSS订阅之jplogic在线RSS阅读器
- 求RSS 阅读器开发资料
- 使用ReactiveCocoa开发RSS阅读器
- 微软在线RSS阅读器抢先预览
- 一个ajax作的RSS在线阅读器
- 一个好用的在线RSS阅读器
- Atitit rss没落以及替代品在线阅读器
- windows phone 开发Rss阅读器教程
- RSS阅读器
- RSS阅读器
- RSS阅读器
- 基于ASP.NET AJAX技术开发在线RSS阅读器(上篇)
- 基于ASP.NET AJAX技术开发在线RSS阅读器(下篇)
- 闲来无事写了个在线RSS阅读器
- 基于ASP.NET AJAX技术开发在线RSS阅读器(上篇)
- JavaScript 指针设计错误
- 关于企业管理信息系统
- vc常见的108个问题
- 感谢上帝,终于能进股沟文档了
- HttpClient入门【转】
- 利用Struts开发Rss在线阅读器
- 搜索引擎当前主流正文提取的思路
- C++ Header file——以备查用
- C#版正文抽取所需正则全集
- 蒙板
- shell程序设计(二)
- ps cs2学习网站2
- 【转】找工作的20条吐血经验
- 网页正文抽取中的网页编码字符集自动识别最佳方案