Java HTML Jsoup Element findAllNodesBefore(Element firstChapter)

Here you can find the source of findAllNodesBefore(Element firstChapter)

Description

find All Nodes Before

License

Open Source License

Declaration

public static List<Node> findAllNodesBefore(Element firstChapter) 

Method Source Code


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

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

public class Main {
    public static List<Node> findAllNodesBefore(Element firstChapter) {
        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;
                }//from  www  . jav  a2s  .  c o  m
            }
            foundNodes.add(node);
        }
        return foundNodes;
    }

    public static List<Node> getAllNodes(Element element) {
        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);
        }
    }
}

Related

  1. createSafeElement(Element sourceEl)
  2. elementsToList(Elements elements)
  3. extractTextWithNewlines(Element elem)
  4. filterElementsByTag(List results, Element element, Set tagSet)
  5. findAElementsWithId(Elements elements, String id)
  6. findElementsByTag(Element element, String... tags)
  7. findFirstByTag(Element element, String tag)
  8. findNextElementSibling(final Element startElement, final Predicate condition)
  9. findPreviousH2Element(Element h3)