Advanced Techniques for HTML Parsing using Java Stream from JSilver
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
html
<html>
<body>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
</table>
</body>
</html>
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HTMLParser {
public static void main(String[] args) {
String html = "<html>...</html>"; // Replace with your actual HTML data
Document doc = Jsoup.parse(html);
Elements rows = doc.select("table tr");
rows.stream()
.skip(1) // Skip the table header row
.forEach(row -> {
Elements columns = row.select("td");
String name = columns.get(0).text();
int age = Integer.parseInt(columns.get(1).text());
// Do something with the name and age data
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("==========================");
});
}
}