【问题标题】:How to traverse recursively an HTML tree with Jsoup?如何使用 Jsoup 递归遍历 HTML 树?
【发布时间】:2016-09-19 22:17:25
【问题描述】:

我有一个 html 文件,我想使用第一个 div 标签 the image display the html file structure 遍历文件 我的代码是

public static void ExtractChild(String content) {

    String data = content;
    ArrayList<String> childList = new ArrayList<String>();
    try{
    Document document = Jsoup.parse(data);
    Element div = document.select("div").first();
    Elements divChildren = div.children();
    int size = divChildren.size();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            data = divChildren.get(i).toString();
            
            System.out.println(data);
            ExtractChild(data);
        }
    } else {
        childList.add(data);

    }
    }
    catch(Exception e)
    {System.out.println(e.getMessage());
        
    }

}
}

我得到一个 Html 文件作为 String 首先我捕获第一个 div 标记并获取它的所有子项。在我的形象中,父母有 3 个孩子,第一个孩子有 2 个孩子。 如果没有任何子子项(其他部分),我将添加子项,如果找到子项,则在 If 部分中出现问题,它会重置 i 的值并且它无法回溯。

【问题讨论】:

    标签: java list recursion jsoup


    【解决方案1】:

    您想使用来自 Jsoup API 的 NodeTraversor 及其伴随类 NodeVisitor

    NodeTraversor 递归遍历节点树。每次点击节点的开始标签或结束标签(如果存在)时,它都会调用给定的 NodeVisitor

    示例代码

    public static void main(String[] args) throws IOException {
        String html = "<div id=\"d1\">" + //
                "<div id=\"d1.0\">" + //
                "<div id=\"d1.0.0\">" + //
                "1.0.0" + //
                "</div>" + //
                "<div id=\"d1.0.1\">" + //
                "1.0.1" + //
                "</div>" + //
                "</div>" + //
                "<div id=\"d1.1\">" + //
                "1.1" + //
                "</div>" + //
                "<div id=\"d1.3\">" + //
                "1.3" + //
                "</div>" + //
                "</div>";
    
        List<String> childList = new ArrayList<>();
        NodeVisitor myNodeVisitor = new MyNodeVisitor(childList);
        NodeTraversor traversor = new NodeTraversor(myNodeVisitor);
        Document doc = Jsoup.parse(html);
    
        Element firstDiv = doc.select("div:first-of-type").first();
        if (firstDiv == null) {
            System.err.println("Unable to find any div.");
        } else {
            traversor.traverse(firstDiv);
    
            for (String child : childList) {
                System.out.println(child);
            }
        }
    }
    
    private static class MyNodeVisitor implements NodeVisitor {
    
        private List<String> childList;
    
        public MyNodeVisitor(List<String> childList) {
            if (childList == null) {
                throw new NullPointerException("childList cannot be null.");
            }
    
            this.childList = childList;
        }
    
        @Override
        public void head(Node node, int depth) {
            if (node.childNodeSize() == 0) {
                childList.add(node.toString());
            }
        }
    
        @Override
        public void tail(Node node, int depth) {
    
        }
    }
    

    输出

    1.0.0
    
    1.0.1
    
    1.1
    
    1.3
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 2015-08-26
      相关资源
      最近更新 更多