Element.hasText() メソッドは、ノードが jsoup にテキストを持っているがリンク テキストを含んでいるかどうかをチェックできることを知っています。プレーンテキストがあるかどうかを確認したいだけですか?誰でも私にいくつかの解決策を教えてもらえますか? どうもありがとう
1773 次
1 に答える
7
これには正規表現を使用します。例を次に示します。
final String html = "<p><span>spantext</span></p>"; // p-tag with no own text, but a child which has one
Document doc = Jsoup.parse(html);
// Check if the 'p'-tag has own text
boolean hasText = doc.select("p").is("p:matchesOwn(^$)"); // --> true, p has no own text
要素にテキストがある場合、これは次を返しfalse
ます:
final String html = "<p>owntext<span>spantext</span></p>";
Document doc = Jsoup.parse(html);
// Check if the 'p'-tag has own text
boolean hasText = doc.select("p").is("p:matchesOwn(^$)"); // --> false, p has some own text
別の解決策:
public static boolean hasOwnText(Element element)
{
return !element.ownText().isEmpty();
}
上からのhtmlとdocで:
boolean hasText = hasOwnText(doc.select("p").first())
于 2013-01-31T13:52:51.907 に答える