public static void main(String[] args){
try
{
String s="<html><p><p><p>a</p>b</p>c</p></html>";
Document doc = Jsoup.parse(s);
Elements p = doc.select("p");
**String text= p.get(1).text();**
System.out.println(text);
}catch (Exception e){
}
}
String text=p.get(0).text(); 结果显示: abc
String text=p.get(1).text(); 结果显示: ab
String text=p.get(2).text(); 结果显示: a
请问怎么能单独显示 b 或者 C ?
用ownText方法,可以指返回当前元素的文本内容,不会返回子元素的文本内容。
public String ownText()
Gets the text owned by this element only; does not get the combined text of all children.
For example, given HTML
Hello there now!
, p.ownText() returns "Hello now!", whereas p.text() returns "Hello there now!". Note that the text within the b element is not returned, as it is not a direct child of the p element.