KJ P
KJ P

Reputation: 31

How can I use Jsoup selector "not"

<div id='contents'>
<div class="article_view">
  <div class="article_txt">
      <strong>I don't want to get this point
        <br>I don't want to get this point
        <br>I don't want to get this point
      </strong>

      <div class='articlePhotoC'>
        <img src="" width='500'>
        <span class='t' style='width:480px;'>
          <b>I don't want to get this point </b>
          I don't want to get this point<br>
        </span>
        <div id='adBox' class='txt_ad' style='width:500px;'></div>
      </div>
      From here I want to get--------------
      <br><br>
      <div class='sub_cont_AD08'></div> 
  </div>
</div>

I have no idea how to use notSelector in Java. I tried to do it like this:

  Elements cont = doc.select("div.article_view :not(div.article_view)"); 

but it is not working. The result includes all of "I don't want to get this point". I hope to get only "From here I want to get~~~~".

Thanks!

Upvotes: 3

Views: 746

Answers (1)

luksch
luksch

Reputation: 11712

If you don't need the text "From here I want to get--------------" as well, i.e. you only want to select Elements within the <div class="article_view"> but not <div class="article_txt"> and its children, you can do this:

Elements els = doc.select("div.article_view>*:not(.article_txt)");

This will select all Elements (*) that are direct children (>) of the div with class "article_view" except the ones with class "article_txt".

EDIT

Now, that it has been defined, that the elements you want are indeed children of the div.article_txt element, I need to modify my answer:

Elements els = doc.select("div.article_view>div.article_txt>*:not(strong,div.articlePhotoC)");

This becomes cumbersome, since you now need to define a list of stuff that should not be included. Note the comma between strong and div.articlePhotoC serving as AND operator in CSS

Upvotes: 2

Related Questions