Reputation: 5
Hey I'd like to get attributes of a Feed.
This is my actual code:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(this.getUrlStream());
document.getDocumentElement().normalize();
NodeList nodeList = document.getElementsByTagName("item");
for (int i = 0; i <= nodeList.getLength(); i++) {
Node node = nodeList.item(i);
System.out.println("Node name: " + node.getNodeName());
Element element1 = (Element) node;
System.out.println("title; " + element1.getElementsByTagName("title").item(0).getTextContent());
Element element = (Element) node;
if(node.getNodeType() == Node.ELEMENT_NODE) {
this.title = element.getElementsByTagName("title").item(0).getTextContent();
System.out.println("description" + element.getElementsByTagName("description").item(0).getTextContent());
String attribute = element.getAttribute("src");
System.out.println(attribute);
}
}
} catch (ParserConfigurationException | SAXException | IOException ex) {
ex.printStackTrace();
}
The following part of the code above should find the attribute: "url":
String attribute = element.getAttribute("src");
System.out.println(attribute);
Sysout: Nothing
The Rss-Feed: https://www.spiegel.de/politik/index.rss
Thank you very much in advance!
Upvotes: 0
Views: 167
Reputation: 19546
You are using getAttribute()
on the Element
object of the <item>
tag. However you have to select the <enclosure>
tag first by using the getElementsByTagName()
as you already did. So you have to use something like:
for (...) {
// ...
// "element" is the object for the <item> tag
Element enclosure = (Element)element.getElementsByTagName("enclosure").item(0);
String url = enclosure.getAttribute("url");
}
Upvotes: 1