java - Jsoup get comment before element -


say have html:

<!-- comment --> <div class="somediv"> ... other html </div> <!-- comment 2 --> <div class="somediv"> ... other html </div> 

i'm getting divs class == somediv , scraping them information. i'm doing this:

document doc = jsoup.connect(url).get(); elements elements = doc.select(".somediv"); (element element : elements) {     //scrape stuff } 

within loop, there way comment tag found before particular div.somediv element i'm on?

if isn't possible, should go parsing html structure differently requirement?

thanks advice.

though question few month old here answer completeness. how using previoussibling preceding node. of course in real code want check, whether comment there.

string html = "<!-- comment --><div class=\"somediv\">... other html</div><!-- comment 2 --><div class=\"somediv\">... other html</div>"; document doc = jsoup.parsebodyfragment(html); elements elements = doc.select(".somediv"); (element element : elements) {     system.out.println(((comment) element.previoussibling()).getdata()); } 

this produces:

some comment  comment 2  

(tested jsoup 1.6.1 , 1.6.3)


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -