Friday, April 29, 2016

get image url of rss with rome library

Leave a Comment

I having a rss file in following :

<?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> 

I want to get image's urls.
I use Rome library but not found any solution.
how to get image's url in item with Rome library ?

2 Answers

Answers 1

I for that get image tag , build new rss parser on the following:

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; }  } 

in the class override parseItem method and add code for get image element and add image's url to Enclosures.

then add following line to rome.properties file :

WireFeedParser.classes=[packge name].NewRssParser

Example :

WireFeedParser.classes=ir.armansoft.newscommunity.newsgathering.parser.impl.NewRssParser

Answers 2

Rome wont provide the <image> tag because it does not belong to the namespace it is in. So the feed isn't valid:

line 18, column 3: Undefined item element: image (29 occurrences) [help]             <image>http://www.saipanews.com/media/image/%D8%AA%D9%88%D9%84%D9%8A%D8%A ... 

If the image tag would be in a different namespace, like this:

<image:image>http://www.saipanews.com/media/image/%D8%AA%D9%88%D9%84%D9%8A%D8%AF/2.jpg</image:image> 

You could get foreing markup in this way:

for(SyndEntry entry : feed.getEntries()) {     for (Element element : entry.getForeignMarkup()) {         System.out.println("element: " + element.toString());     } } 

And the result would be

element: [Element: <image:image [Namespace: http://purl.org/rss/1.0/modules/image/]/>] 

Unless the feed is fixed, It seems that there isn't a way to get the image url with Rome library at the moment.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment