Examples of RSS parsers of rome.jar and jdom.jar in Java

  • 2020-04-01 03:01:09
  • OfStack

1. You need two packages, jdom.jar and rome.jar.
2. Create a project, the content of web.xml is as follows:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
 

3. Create an index.jsp as follows:


  The following code   Copy the code  
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sina News</title>
</head>
<body>
</p><%
</p></p>java.util.Properties systemSettings = System.getProperties();
</p></p>systemSettings.put("http.proxyHost", "mywebcache.com");
</p></p>systemSettings.put("http.proxyPort", "8080");
</p></p>System.setProperties(systemSettings);
</p></p>String urlStr = "http://rss.sina.com.cn/news/marquee/ddt.xml";
</p></p>java.net.URLConnection feedUrl = new java.net.URL(urlStr).openConnection();
</p></p>feedUrl.setRequestProperty("User-Agent",
</p></p></p></p>"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
</p></p>com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput();
</p></p>com.sun.syndication.feed.synd.SyndFeed feed = input.build(new com.sun.syndication.io.XmlReader(feedUrl));
</p>%>
</p><div align="center">
</p></p><h1><%=feed.getTitle()%></h1>
</p></p><table border=1 cellpadding=3 width="700">
</p></p></p><tr>
</p></p></p></p><th>Number</th>
</p></p></p></p><th>Title</th>
</p></p></p></p><th>Time www.jb51.net</th>
</p></p></p></p><th>Content</th>
</p></p></p></tr>
</p></p></p><%
</p></p></p></p>java.util.List list = feed.getEntries();
</p></p></p></p>for (int i = 0; i < list.size(); i++) {
</p></p></p></p></p>com.sun.syndication.feed.synd.SyndEntry entry = (com.sun.syndication.feed.synd.SyndEntry) list.get(i);
</p></p></p></p>%>
</p></p></p><tr>
</p></p></p></p><td><%=i + 1%></td>
</p></p></p></p><td><a href="<%=entry.getLink()%>"><%=entry.getTitle()%></a></td>
</p></p></p></p><td><%=entry.getPublishedDate()%></td>
</p></p></p></p><td><%=entry.getDescription().getValue() %></td>
</p></p></p></tr>
</p></p></p><%
</p></p></p></p>}
</p></p></p>%>
</p></p></table>
</p></div>
</p><br>
</body>
</html>


Related articles: