无法使用Rome API从Feed中读取图片网址 [英] Unable to read image URL from feed using Rome API
问题描述
我正在使用ROME解析器来解析我的RSS / Atom提要。现在的问题是它没有提供新闻提要/条目的图像URL。问题的部分原因还在于提要不一致,并且他们将图片网址不一致。
I am using ROME parser to parse my RSS/Atom feeds. Now the problem is that it doesn't give image URL of the news feed/entry. Part of the problem is also because feeds are not consistent and they put image urls inconsistently.
BBC新闻将图片网址放在< media:thumbnail ...>
元素
BBC news puts image url inside <media:thumbnail...>
element
<item>
<title>Dementia in care homes 'more common'</title>
<description>Eight out of 10 residents in care homes are now thought to have dementia or severe memory problems, new data shows.</description>
<link>http://www.bbc.co.uk/news/health-21579394#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/health-21579394</guid>
<pubDate>Tue, 26 Feb 2013 00:28:31 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/66064000/jpg/_66064884_c0016428-geriatric_care-spl.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/66064000/jpg/_66064885_c0016428-geriatric_care-spl.jpg"/>
</item>
但是有些新闻提要将图片放在 enclosure 元素中。而且有些Feed根本没有它们。
But some of the news feeds put images inside enclosure element. And some of the feeds don't have them at all.
所以我的问题是;如果它们出现在Feed中,我怎么能得到它们。到目前为止,罗马API一直在为我工作;但现在我陷入了困境。
So my problem is; how can i get them if they are present in the feed. So far Rome API has been working perfectly for me; but now I am stuck at this.
推荐答案
我可以找出从Feed中获取图片网址的方法。部分问题是因为罗马没有使用泛型;因此无法正确读取< media:thumbnail ..
元素,因此丢失了作为属性的图像的url。
I could figure out ways to get image url from the feed. Part of the problem is because Rome doesn't use Generics; so was not able to read <media:thumbnail..
element properly and hence was loosing the url of the image which comes as attribute.
调试后我可以找出确切的参数化类型,然后很容易:)
After debugging i could figure out exact Parameterized type and then it was easy :)
List<Element> foreignMarkups = (List<Element>) entry.getForeignMarkup();
for (Element foreignMarkup : foreignMarkups) {
String imgURL = foreignMarkup.getAttribute("url").getValue();
//read width and height
}
还有什么我找到了一些新闻提要;图像的url位于Enclosure元素内,如下所示:
Also what i have found is for some of the news feeds; url of the image is inside Enclosure element like below:
< enclosure url =http://www.wired.com/reviews/ wp-content / uploads / 2013/02 / lights_remote_1-200x100.jpgtype =image / jpeglength =48000/>
所以我也在检查 enclosure
元素,如果< media:thumbnail ..
元素不存在于Feed:
So i am also checking in enclosure
element if <media:thumbnail..
element is not present in the feed:
List<SyndEnclosure> encls = entry.getEnclosures();
if(!encls.isEmpty()){
for(SyndEnclosure e : encls){
String imgURL = e.getUrl().toString();
}
}
这篇关于无法使用Rome API从Feed中读取图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!