How To Get Text From This Html Tag By Using Jsoup?
I meet a position when i using jsoup to extracting data. The data like this: This is a strong number 2013 I want to get data
Solution 1:
You can parse the html into a Document
, select the body
-Element and get its text.
Example:
Document doc = Jsoup.parse("This is a <strong>strong</strong> number <date>2013</date>");
String ownText = doc.body().ownText();
Stringtext = doc.body().text();
System.out.println(ownText);
System.out.println(text);
Output:
This is a number
This is astrong number 2013
Solution 2:
This should answer your question :
publicStringescapeHtml(String source) {
Document doc = Jsoup.parseBodyFragment(source);
Elements elements = doc.select("b");
for (Element element : elements) {
element.replaceWith(newTextNode(element.toString(),""));
}
returnJsoup.clean(doc.body().toString(), newWhitelist().addTags("a").addAttributes("a", "href", "name", "rel", "target"));
}
Jsoup - Howto clean html by escaping not deleting the unwanted html?
Post a Comment for "How To Get Text From This Html Tag By Using Jsoup?"