Java HTML Jsoup Element findPreviousH2Element(Element h3)

Here you can find the source of findPreviousH2Element(Element h3)

Description

find Previous H Element

License

Open Source License

Declaration

public static Element findPreviousH2Element(Element h3) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

public class Main {
    public static Element findPreviousH2Element(Element h3) {
        Elements hElements = findAllHElements(h3);
        Element currentH2 = null;
        for (Element h : hElements) {
            if (h.nodeName().equals("h2")) {
                currentH2 = h;//from   w w  w  . ja v  a  2  s .com
            }
            if (h.html().equals(h3.html())) {
                return currentH2;
            }
        }
        return null;
    }

    private static Elements findAllHElements(Element element) {
        Element root = findRootElement(element);
        return root.select("h1,h2,h3");
    }

    private static Element findRootElement(Element element) {
        Element parent;
        do {
            parent = element.parent();
            if (parent == null) {
                return element;
            }
            element = parent;
        } while (parent != null);
        return null;
    }
}

Related

  1. findAElementsWithId(Elements elements, String id)
  2. findAllNodesBefore(Element firstChapter)
  3. findElementsByTag(Element element, String... tags)
  4. findFirstByTag(Element element, String tag)
  5. findNextElementSibling(final Element startElement, final Predicate condition)
  6. findPreviousHElement(Elements sequence, Element reference)
  7. findRootElement(Element element)
  8. firstElement(final Element element, final String... tags)
  9. getActiveJob(Element element)