문제

I want to select all comments from a document using JSoup. I would like to do something like this:

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

I have tried this:

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

  }

}

But the following error occurs in eclipse "Incompatible conditional operand types Element and Comment".

Cheers,

Pete

도움이 되었습니까?

해결책

Since Comment extends Node you need to apply instanceof to the node objects, not the elements, like this:

    for(Element e : doc.getAllElements()){
        for(Node n: e.childNodes()){
            if(n instanceof Comment){
                System.out.println(n);
            }
        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top