1

In a Java application, imagine I have this somewhere in a huge HTML code:

<h2>
   <b>
      This
   </b>
</h2>

<h1>
</h1>

So imagine I want to get the first element h1 after the element b. I've tried some CSS selectors through the JSoup library but found nothing useful. The greatest problem here is: They are not siblings. I don't know if the b element is or is not a child of a different parent.

Is there a way of doing it?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Mr Guliarte
  • 739
  • 1
  • 10
  • 27
  • 1
    Looking for a class, instead of an `h1`, but the logic and problem are the same: http://stackoverflow.com/questions/1827482/jquery-find-next-prev-elements-of-a-certain-class-but-not-necessarily-siblings – Paul Roub Jul 08 '15 at 17:40
  • are you trying to target it for a js script or in a css sheet? – Rooster Jul 08 '15 at 17:40
  • I don't typically recommend libraries since I'm a firm believer in vanilla js, but, jQuey is really, really useful for DOM traversal. – Howard Renollet Jul 08 '15 at 17:43
  • Your question is unclear: Are you trying to: 1) Use just CSS, 2) Use JavaScript's DOM API (possibly with CSS selectors), or 3) Use JSoup (not in a browser)? – T.J. Crowder Jul 08 '15 at 17:44
  • Sorry for not letting it clear. I want to achieve it through CSS,JS or JSOUP, specifically. – Mr Guliarte Jul 08 '15 at 17:47
  • 3
    That's still not clear: What environment are you using? A browser? A Java application? What? JSoup wouldn't be relevant to a browser. – T.J. Crowder Jul 08 '15 at 17:48
  • 1
    Not possible with CSS for the reason bolded in the question. – Paulie_D Jul 08 '15 at 17:48
  • thuper unclear. those are such different use cases... – Rooster Jul 08 '15 at 17:49
  • 1
    It's a Java application, but Jsoup has a method that searches the HTML document with CSS selectors. – Mr Guliarte Jul 08 '15 at 17:49
  • Please don't confuse Java with JavaScript. – BoltClock Jul 08 '15 at 17:56
  • Your question was originally tagged [javascript] instead of [java]. – BoltClock Jul 08 '15 at 18:02
  • Anyway, are you able to identify this `b` element? If so, a starting point in Java code would be useful. – BoltClock Jul 08 '15 at 18:07
  • I see now why this question was deemed unclear (and why you originally tagged your question [javascript]) - you state that you want to achieve this through either JS, CSS or jsoup, but jsoup can't execute JavaScript or apply CSS, so what is not clear (at least to me anyway) is how exactly you intend to do this in JS or CSS. – BoltClock Jul 08 '15 at 18:18
  • Sorry, then. Yes, I'm able to find the B element (in the real world, it has an ID). I mean, I can do this in Java with a linear search through the HTML document, but I wanted to keep it more simple manipulating it as a DOM tree. – Mr Guliarte Jul 08 '15 at 18:19
  • JSoup has a "Select" method that uses JS/CSS selectors to search through the document...that's why any of the solutions would fit. Sorry for no letting it clear. – Mr Guliarte Jul 08 '15 at 18:21

1 Answers1

1

You can't do it purely with CSS selectors, you'll have some logic you'll have to perform in your code using JSoup.

This algorithm would work on a browser as well as a JSoup document. Basically, starting from the b element, here's how you find the h1:

  1. Set current to b.
  2. While current is not null:
    1. Set sibling to current's next element sibling (in JSoup, that would be the nextElementSibling method; in any vaguely modern browser, it's the nextElementSibling property.
    2. While sibling is not null:
      1. If sibling is an h1, you're done
      2. Otherwise, set sibling to sibling's next element sibling and continue this sibling loop (the "while sibling is not null")
    3. (sibling is null) Set current to current's parent node. In JSoup, that would be the parent method. In a browser, it would be the parentNode property.
    4. If current is not null, continue this parent loop (the "while current is not null").
    5. There was no following h1.
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks! But does this solutions finds "nephews"? It seems that, since I have a current element, I can only find elements that are at the same level it is or any above, but not any under. – Mr Guliarte Jul 08 '15 at 18:25
  • @MrGuliarte: This finds any *following* nephews, yes. – T.J. Crowder Jul 09 '15 at 05:05