-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimalRSS.java
More file actions
36 lines (29 loc) · 1.29 KB
/
MinimalRSS.java
File metadata and controls
36 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.net.*;
public class MinimalRSS {
public static void main(String[] args) {
try {
// Load RSS
String url = "https://towardsdatascience.com/feed/";
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(new URL(url).openStream());
doc.getDocumentElement().normalize();
// Get all articles
NodeList items = doc.getElementsByTagName("item");
System.out.println("📰 TOWARDS DATA SCIENCE - Latest Articles\n");
// Show titles only
for (int i = 0; i < Math.min(15, items.getLength()); i++) {
Element item = (Element) items.item(i);
String title = item.getElementsByTagName("title").item(0).getTextContent();
String link = item.getElementsByTagName("link").item(0).getTextContent();
System.out.println((i + 1) + ". " + title);
System.out.println(" 🔗 " + link);
System.out.println();
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}