In this post under Jsoup, I will show with example how to get a list of all html elements in a document.
Below is the main class
Main Class
1 import java.io.File;
2 import java.io.IOException;
3 import java.util.Iterator;
4
5 import org.jsoup.Jsoup;
6 import org.jsoup.nodes.Document;
7 import org.jsoup.nodes.Element;
8 import org.jsoup.select.Elements;
9
10 public class JsoupDemo4 {
11 public static void main(String[] args) throws IOException {
12 File file = new File("Input1.html");
13 Document document = Jsoup.parse(file, "UTF-8");
14 Elements elements = document.getAllElements();
15 Iterator<Element> iterator = elements.iterator();
16 while(iterator.hasNext()) {
17 Element element = iterator.next();
18 System.out.println(element.nodeName());
19 }
20 }
21 }
As you can see in the above code, at line 13 we parse the html file using Jsoup’s static method “parse”. This method will return a Document object.
At line 14, we call “getAllElements” method on the Document object. This will return all the html elements used in the file.
In this way, we can get a list of all html elements used in the html file.