How to search for comments (“<!— -->”) using Jsoup?

前端 未结 4 1141
栀梦
栀梦 2020-12-31 07:03

I would like to remove those tags with their content from source HTML.

4条回答
  •  猫巷女王i
    2020-12-31 07:42

    reference @dlamblin https://stackoverflow.com/a/7541875/4712855 this code get comment html

    public static void getHtmlComments(Node node) {
        for (int i = 0; i < node.childNodeSize();i++) {
            Node child = node.childNode(i);
            if (child.nodeName().equals("#comment")) {
                Comment comment = (Comment) child;
                child.after(comment.getData());
                child.remove();
            }
            else {
                getHtmlComments(child);
            }
        }
    }
    

提交回复
热议问题