JSoup - 选择所有评论
-
27-09-2019 - |
题
我要选择使用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);
}
}
}
不隶属于 StackOverflow