Java Utililty Methods HTML Jsoup Element

List of utility methods to do HTML Jsoup Element

Description

The list of methods to do HTML Jsoup Element are organized into topic(s).

Method

ListfindAllNodesBefore(Element firstChapter)
find All Nodes Before
List<Node> foundNodes = new ArrayList<>();
List<Node> parentNodes = getAllNodes(firstChapter.parent());
for (Node node : parentNodes) {
    if (node instanceof Element) {
        Element element = (Element) node;
        if ("h1".equals(element.tagName())) {
            return foundNodes;
    foundNodes.add(node);
return foundNodes;
ElementsfindElementsByTag(Element element, String... tags)
Returns the all elements matching any of the given tags (case-insensitive).
List<Element> results = new ArrayList<Element>();
Set<String> tagSet = new HashSet<String>();
for (String tag : tags)
    tagSet.add(tag.toLowerCase());
filterElementsByTag(results, element, tagSet);
return new Elements(results);
ElementfindFirstByTag(Element element, String tag)
Returns the first element found with the given tag (or tag sequence separated by '/') or null.
return findFirstByTag(element, tag.split("/"), 0);
OptionalfindNextElementSibling(final Element startElement, final Predicate condition)
find Next Element Sibling
for (Element el = startElement.nextElementSibling(); el != null; el = el.nextElementSibling()) {
    if (condition.test(el)) {
        return Optional.of(el);
return Optional.empty();
ElementfindPreviousH2Element(Element h3)
find Previous H Element
Elements hElements = findAllHElements(h3);
Element currentH2 = null;
for (Element h : hElements) {
    if (h.nodeName().equals("h2")) {
        currentH2 = h;
    if (h.html().equals(h3.html())) {
        return currentH2;
...
ElementfindPreviousHElement(Elements sequence, Element reference)
find Previous H Element
int index = sequence.indexOf(reference);
for (index = sequence.indexOf(reference); index >= 0; index--) {
    Element element = sequence.get(index);
    if (element.tagName().startsWith("h")) {
        return element;
return null;
...
ElementfindRootElement(Element element)
find Root Element
Element parent;
do {
    parent = element.parent();
    if (parent == null) {
        return element;
    element = parent;
} while (parent != null);
...
ElementfirstElement(final Element element, final String... tags)
first Element
Element _element = element;
for (String tag : tags)
    _element = firstChildByTag(_element, tag);
return _element;
StringgetActiveJob(Element element)
get Active Job
if (element.select("dd.current-content").size() > 0) {
    return element.select("dd.current-content").get(0).text();
return null;
ListgetAllNodes(Element element)
get All Nodes
try {
    Field childNodesField = Node.class.getDeclaredField("childNodes");
    childNodesField.setAccessible(true);
    @SuppressWarnings("unchecked")
    List<Node> childNodes = (List<Node>) childNodesField.get(element);
    return childNodes;
} catch (Exception e) {
    throw new RuntimeException("Could not get the nodes of element: " + element, e);
...