简体   繁体   中英

Using jSoup to find a node with some particular text

How can I find this snippet of HTML in a node using jSoup:

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

I'd like to extract the date from the HTML snippet. The problem is that this snippet of HTML can occur anywhere within an Element so I need to match it using the contained text.

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());

As you have tagged the question "xpath", I am going to assume that you will accept an XPATH solution. In the absence of information to the contrary, I will make some reasonable assumptions. Please let us know if you want to correct or refine these assumptions.

Assumptions

  1. The is exactly one span element in the document with text value 'Party Date:' .
  2. The 'Part Date:' text is exactly as is. Never with leading or trailing white-space nor variation in case.
  3. The text node following the said span contains the target value.
  4. The said span element can occur anywhere in the document.
  5. The style attribute is immaterial to the question.

XPath expression

The following XPATH expression...

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

...returns...

' 14.08.2012'

Note: This works in both XPATH 1.0 and XPATH 2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM