我想使用 JSoup 从文档中选择所有评论。我想做这样的事情:
for(Element e : doc.select("comment")) {
System.out.println(e);
}
我试过这个:
for (Element e : doc.getAllElements()) {
if (e instanceof Comment) {
}
}
但是eclipse中出现以下错误“不兼容的条件操作数类型元素和注释”。
干杯,
皮特
我想使用 JSoup 从文档中选择所有评论。我想做这样的事情:
for(Element e : doc.select("comment")) {
System.out.println(e);
}
我试过这个:
for (Element e : doc.getAllElements()) {
if (e instanceof Comment) {
}
}
但是eclipse中出现以下错误“不兼容的条件操作数类型元素和注释”。
干杯,
皮特
由于Comment extends Node
您需要应用于instanceof
节点对象,而不是元素,如下所示:
for(Element e : doc.getAllElements()){
for(Node n: e.childNodes()){
if(n instanceof Comment){
System.out.println(n);
}
}
}
在Kotlin中,您可以通过 Jsoup 获取Comment
全部Document
或特定内容Element
:
fun Element.getAllComments(): List<Comment> {
return this.allElements.flatMap { element ->
element.childNodes().filterIsInstance<Comment>()
}
}