List of utility methods to do HTML Jsoup Element
List | findAllNodesBefore(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; |
Elements | findElementsByTag(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); |
Element | findFirstByTag(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); |
Optional | findNextElementSibling(final Element startElement, final Predicate 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(); |
Element | findPreviousH2Element(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; ... |
Element | findPreviousHElement(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; ... |
Element | findRootElement(Element element) find Root Element Element parent; do { parent = element.parent(); if (parent == null) { return element; element = parent; } while (parent != null); ... |
Element | firstElement(final Element element, final String... tags) first Element Element _element = element; for (String tag : tags) _element = firstChildByTag(_element, tag); return _element; |
String | getActiveJob(Element element) get Active Job if (element.select("dd.current-content").size() > 0) { return element.select("dd.current-content").get(0).text(); return null; |
List | getAllNodes(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); ... |