I can see why the two iterate methods get caught in an infinite loop calling each other but can't see how to reasonably prevent this. I suppose I could have a counter to simply limit the depth. But, would it not be possible to determine that the bottom of the link depth has been reached? To dynamically detect that there's nothing more to process?

I'm looking for some way to build a tree, if that even makes sense, of the elements.


package my.books;

import java.io.File;
import java.net.URI;
import java.util.Properties;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class App {

    private static final Logger LOG = Logger.getLogger(App.class.getName());
    private Properties properties = new Properties();

    public static void main(String[] args) throws Exception {
        new App().basicJSoup();

    private void basicJSoup() throws Exception {
        URI inputURI = new URI(properties.getProperty("html_input"));
   //     URI outputURI = new URI(properties.getProperty("output"));

        File input = new File(inputURI);
        Document doc = Jsoup.parse(input, "UTF-8");
        Elements sideCategory = doc.select("div.side_categories");
        //  Elements links = doc.select("div.side_categories ul li a");

    private void iterate(Elements elements) {
        Element element = null;
        for (int i = 0; i < elements.size(); i++) {
            element = elements.get(i);

    private void iterate(Element element) {
        Elements elements = element.getAllElements();

stack trace:

thufir@dur:~/NetBeansProjects/books$ gradle run

> Task :run FAILED
Exception in thread "main" java.lang.StackOverflowError
        at org.jsoup.select.Collector.collect(Collector.java:27)
        at org.jsoup.nodes.Element.getAllElements(Element.java:1011)
        at my.books.App.iterate(App.java:43)

Try to use this (children()) instead of getAllElements(). Because when you use the latter one, you get the node itself alongside with its children. Hence you will always be cycling between two methods.

  • let me try; makes sense. thx.
    – Thufir
    Jan 22 '19 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.