用rome生成rss时报java.lang.NoSuchMethodError: org.jdom.Element.addContent(Ljava/lang/String;)

一、原因是由于jdom包不对,记住一定要用rome1.0.jar包和jdom.jar包,这两个包可以通过以下链接去下载

rome和jdom包下载

---------------------------------------------------------------------------------------------

二、下面我们看看如何用rome生成和解析RSS


(一)、生成RSS

1、生成rss格式的xml

2主要使用的类

Channel

Item

Description

WireFeedOutput

3、主要使用的方法

(1)Channel下的方法

newChannel("rss_2.0");格式一定得是rss_2.0

setTitle

setDescription

setLink

setEncoding

(2)Item下的方法

setAuthor

setTitle

setLink

setDescription

(3)Description下的方法

setType

setValue

4、代码片段

Channel channel = new Channel("rss_2.0");
channel.setTitle("Rss web site");
channel.setDescription("channel的描述");
channel.setLink("http://localhost:8080/app/rss/1.xml");
channel.setEncoding("GBK");

// 这个list对应rss中的item列表
List items = new ArrayList();
// 新建Item对象,对应rss中的<item></item>
Item item = new Item();
item.setAuthor("yangchen");
item.setTitle("this is my first rss");
item.setLink(url + "/index.html");

// 新建一个Description,它是Item的描述部分
Description description = new Description();
description.setType("item description type");
description.setValue("welcome to rss's world");
item.setDescription(description);

items.add(item);
channel.setItems(items);
WireFeedOutput wireFeedOutput = new WireFeedOutput();
wireFeedOutput.output(channel);

(二)、解析RSS

1解析RSS格式的XML

2、主要使用到的类

URLConnection

SyndFeedInput

SyndFeed

3SyndFeed的类下的方法

getTitle()获取网站大标题,如腾讯的RSS网站中“新闻国内”

getEntries()获取每个子项目

获取子项目对象后有以下方法

getTitle()获取新闻标题

getLink()获取新闻的链接

getPublishedDate()获取新闻发布的日期

4、代码片段

URLConnectionfeedUrl =newURL(jURL).openConnection();

SyndFeedInputinput =newSyndFeedInput();

SyndFeedfeed = input.build(newXmlReader(feedUrl));

//System.out.println(feed.getTitle());

Listlist = feed.getEntries();

for(inti = 0; i < list.size(); i++)

{

SyndEntryentry = (SyndEntry) list.get(i);

System.out.println("标题------>>>"+ entry.getTitle());

System.out.println("链接------>>>"+ entry.getLink());

System.out.println("出版日期------>>>"+ entry.getPublishedDate());

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值