Android 从Rss获取图像--最好的方法是什么?
我从一个网站获得Rss。在description标签中有一个图像,我想向用户显示。我正在使用此代码获取它:Android 从Rss获取图像--最好的方法是什么?,android,rss,Android,Rss,我从一个网站获得Rss。在description标签中有一个图像,我想向用户显示。我正在使用此代码获取它: public void setDescription(String description) { this.description = description; //parse description for any image or video links if (description.contains("<img style=")
public void setDescription(String description) {
this.description = description;
//parse description for any image or video links
if (description.contains("<img style=")){
String imgg = description.substring(description.indexOf("<img style="));
String cleanUp = imgg.substring(0, imgg.indexOf(">")+1);
imgg = imgg.substring(imgg.indexOf("src=") + 5);
int indexOf = imgg.indexOf("\"");
if (indexOf==-1){
indexOf = imgg.indexOf("\"");
}
imgg = imgg.substring(0, indexOf);
setImgLink(imgg);
this.description = this.description.replace(cleanUp, "");
} else{
String imgg2="replace_image.png";
setImgLink(imgg2);
}
}
public void setDescription(字符串描述){
this.description=描述;
//解析任何图像或视频链接的描述
if(description.contains()
我的问题是,如果我获取rss的站点更改了xml中的某些内容,我将获取替换图像
有没有办法修复它,每次从描述中获取图像??
我发现我可以用这样的东西,但我不能让它工作
谢谢!:)与其使用正则表达式,不如使用DOM和XPATH来提取图像源。这样,即使HTML发生变化,您的解决方案仍能工作
您似乎在description
中有描述,并且希望图像路径位于imgg
中。请尝试以下操作:
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(description);
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//img";
Node imgNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
String imgg = imgNode.getAttribute("src");
什么是节点导入(我指的是库)?这是一个导入org.w3c.dom.Node;请指定正在使用的库。我无法在w3c节点中找到getAttribute
。类型节点的方法getAttribute(字符串)未定义