Here you can find the source of findPreviousH2Element(Element h3)
public static Element findPreviousH2Element(Element h3)
//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; } }