Party ...">

Using jSoup to search for a node with specific text

How to find this HTML snippet in node using jSoup:

<span style="font-weight: bold">Party Date:</span> 14.08.2012<br>

I want to extract a date from an HTML snippet. The problem is that this piece of HTML can happen anywhere inside the element, so I need to map it using the contained text.

+5
source share
2 answers

If you are still looking for jsoup selector query .. this works for me ..

    String html = "<span style=\"font-weight: bold\">Party Date:</span> 14.08.2012<br>";

    System.out.println("Date " + Jsoup.parse(html).select("span:matchesOwn(Party Date:)").first().nextSibling().toString());
+14
source

As you noted the question "xpath", I am going to assume that you will decide XPATH. In the absence of information to the contrary, I will make some reasonable assumptions. Let us know if you want to correct or clarify these assumptions.

Assumptions

  • span " :".
  • " ": , . .
  • node, , .
  • span .
  • .

XPath

XPATH...

//span[.='Party Date:'][1]/following-sibling::text()

... ...

' 14.08.2012'

. XPATH 1.0, XPATH 2.0

+1

All Articles