我要选择使用JSoup一个文档中的所有评论。我愿做这样的事情:

for(Element e : doc.select("comment")) {
   System.out.println(e);
}

我曾尝试这样的:

for (Element e : doc.getAllElements()) {
  if (e instanceof Comment) {

  }

}

但在蚀会出现以下错误“不兼容的条件的操作数类型的元素和注释”。

干杯,

皮特

有帮助吗?

解决方案

由于Comment extends Node需要应用instanceof到节点对象,而不是元素,如下所示:

    for(Element e : doc.getAllElements()){
        for(Node n: e.childNodes()){
            if(n instanceof Comment){
                System.out.println(n);
            }
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top