views:

20

answers:

1

Hey,

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

+1  A: 

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);
            }
        }
    }
dogbane