Java 来自RSS的图像URL
我有一个显示RSS提要的应用程序。我能够获得项目标题、日期和链接,没有问题,但我正试图找出如何获得缩略图URL 编辑:我正在获取URL,但我的URL字符串数组只填充了第一个URL。它只是在阵列中存储了22次 以下是带有图像URL的提要中的XML:Java 来自RSS的图像URL,java,android,xml,rss,Java,Android,Xml,Rss,我有一个显示RSS提要的应用程序。我能够获得项目标题、日期和链接,没有问题,但我正试图找出如何获得缩略图URL 编辑:我正在获取URL,但我的URL字符串数组只填充了第一个URL。它只是在阵列中存储了22次 以下是带有图像URL的提要中的XML: <media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://kyfbnewsroom.com/wp-content/uploads/2012/09/Beltwa
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://kyfbnewsroom.com/wp-content/uploads/2012/09/Beltway-News-web-250x250.jpg" width="250" height="250"/>
这是我的密码:
public void getDataFromFeed(){
try {
Feed = new URL(URLFeed);
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse(new InputSource(Feed.openStream()));
doc.getDocumentElement().normalize();
nodeList = doc.getElementsByTagName("item");
title = new String[nodeList.getLength()];
pubDate = new String[nodeList.getLength()];
link = new String[nodeList.getLength()];
thumb = new String[nodeList.getLength()];
for(int i=0;i<nodeList.getLength();i++){
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList titleList = fstElmnt.getElementsByTagName("title");
Element titleElement = (Element) titleList.item(0);
titleList = titleElement.getChildNodes();
title[i] = ((Node) titleList.item(0)).getNodeValue();
NodeList pubDateList = fstElmnt.getElementsByTagName("pubDate");
Element pubDateElement = (Element) pubDateList.item(0);
pubDateList = pubDateElement.getChildNodes();
pubDate[i] = ((Node) pubDateList.item(0)).getNodeValue();
// chops off the " +0000" on date
pubDate[i] = pubDate[i].substring(0, pubDate[i].length()-14);
NodeList thumbList = doc.getDocumentElement().getElementsByTagName("media:thumbnail");
Element thumbElement = (Element)thumbList.item(0);
String thumbURL = thumbElement.getAttribute("url");
thumb[i] = thumbURL;
NodeList linkList = fstElmnt.getElementsByTagName("link");
Element linkElement = (Element) linkList.item(0);
linkList = linkElement.getChildNodes();
link[i] = ((Node) linkList.item(0)).getNodeValue();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getDataFromFeed(){
试一试{
Feed=新URL(URLFeed);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
doc=db.parse(新的InputSource(Feed.openStream());
doc.getDocumentElement().normalize();
nodeList=doc.getElementsByTagName(“项目”);
title=新字符串[nodeList.getLength()];
pubDate=新字符串[nodeList.getLength()];
link=新字符串[nodeList.getLength()];
thumb=新字符串[nodeList.getLength()];
对于(int i=0;i如果您使用的是org.w3c.dom,请执行以下操作:
NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("media", "thumbnail");
// if you expect there to possibly be more than one use a loop or whatever
Element elm = (Element) nodes.item(0);
String url = elm.getAttribute("url");
查看我的更新。我已添加此内容,但字符串url导致其崩溃。我已修复该问题。我只需将(“媒体”、“缩略图”)更改为(“媒体:缩略图”)我的URL数组中有22个对象,但它们都是空的。没关系。我应该将thumbURL字符串添加到我的数组中。我现在正在获取URL。我刚刚注意到我的URL数组中每个索引都填充了相同的URL。你知道为什么吗?