gpt4 book ai didi

java - 来自 RSS 的图像 URL

转载 作者:行者123 更新时间:2023-12-01 12:39:04 25 4
gpt4 key购买 nike

我有一个显示 RSS 源的应用程序。我能够毫无问题地获取项目标题、日期和链接,但我正在尝试找出如何获取缩略图 URL。

编辑:我正在获取 URL,但我的 URL 字符串数组仅填充了第一个 URL。它只是在数组中存储了 22 次。

以下是带有图像 URL 的 Feed 中的 XML:

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

最佳答案

如果您使用 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");

关于java - 来自 RSS 的图像 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25289539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com