我有以下 rss 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title> ????? ???? </title>
<link>http://www.saipanews.com/</link>
<description></description>
<language>fa</language>
<item>
<author></author>
<pretitle></pretitle>
<title>???? ????? ???? ????? ?? ?????? ???????? ?????? ????? ???? ???(?) ? ??? ???</title>
<link>http://www.saipanews.com/view-6751.html</link>
<pubdate>2016-04-20 10:58:00</pubdate>
<description>????? ????: ???????? ???? ????????? ????? ?????? ?? ???????? ?????? ????? ??????? ???? ???(?) ? ??? ???? ?? ????? ?? ????? ????? ????? ? ?????? ????? ???? ????? ???.</description>
<secid>0</secid>
<typid>8</typid>
<image>http://www.saipanews.com/media/image/jamali/jmali.JPG</image>
</item>
<item>
<author></author>
<pretitle></pretitle>
<title>????? ??????? ??? ???? ?? ????? ???? ?? ??? ????? ??? </title>
<link>http://www.saipanews.com/view-6748.html</link>
<pubdate>2016-04-19 11:27:00</pubdate>
<description>????? ????: ?? ????? ????? ???? ? ?? ??? ?? ?????? ?? ?????? ?? ????? ??????? ?????? ?? ?? ???????? ???? ??????? ? ???? ????? ??????? ?????? ???? ?? ????? ? ???? ?? ???? ??????? ?? ??? ???? ????? ???? ??? ?? ??????? ????? ??????? ?? ??????? ????? ????? ? ????? ????? ?? ????.</description>
<secid>0</secid>
<typid>8</typid>
<image>http://www.saipanews.com/media/image/farhang%20ranandegi/252887_331.jpg</image>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
我想获取图片的网址。
我使用罗马图书馆,但没有找到任何解决方案。
如何使用罗马图书馆在项目中获取图像的网址?
我为了获得图像标签,在以下内容上构建新的 rss 解析器:
public class NewRssParser extends RSS094Parser implements WireFeedParser {
public NewRssParser() {
this("rss_2.0");
}
protected NewRssParser(String type) {
super(type);
}
protected String getRSSVersion() {
return "2.0";
}
protected boolean isHourFormat24(Element rssRoot) {
return false;
}
protected Description parseItemDescription(Element rssRoot, Element eDesc) {
Description desc = super.parseItemDescription(rssRoot, eDesc);
desc.setType("text/html"); // change as per
// https://rome.dev.java.net/issues/show_bug.cgi?id=26
return desc;
}
public boolean isMyType(Document document) {
boolean ok;
Element rssRoot = document.getRootElement();
ok = rssRoot.getName().equals("rss");
if (ok) {
ok = false;
Attribute version = rssRoot.getAttribute("version");
if (version != null) {
// At this point, as far ROME is concerned RSS 2.0, 2.00 and
// 2.0.X are all the same, so let's use startsWith for leniency.
ok = version.getValue().startsWith(getRSSVersion());
}
}
return ok;
}
@Override
public Item parseItem(Element arg0, Element arg1) {
Item item = super.parseItem(arg0, arg1);
Element imageElement = arg1.getChild("image", getRSSNamespace());
if (imageElement != null) {
String imageUrl = imageElement.getText();
Element urlElement = imageElement.getChild("url");
imageUrl = urlElement != null ? urlElement.getText() : imageUrl;
Enclosure enc = new Enclosure();
enc.setType("image");
enc.setUrl(imageUrl);
item.getEnclosures().add(enc);
}
return item;
}
}
Run Code Online (Sandbox Code Playgroud)
在类中覆盖 parseItem 方法并添加获取图像元素的代码并将图像的 url 添加到 Enclosures。
然后将以下行添加到rome.properties文件:
WireFeedParser.classes=[包名称].NewRssParser
例子 :
WireFeedParser.classes=ir.armansoft.newscommunity.newsgathering.parser.impl.NewRssParser
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |