简体   繁体   中英

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>strong</strong> number <date>2013</date>

I want to get data like this: This is a number

How can I do that? Can anyone help me?

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();
String text = doc.body().text();

System.out.println(ownText);
System.out.println(text);

Output:

This is a number  
This is a strong number 2013

This should answer your question :

public String escapeHtml(String source) {
    Document doc = Jsoup.parseBodyFragment(source);
    Elements elements = doc.select("b");
    for (Element element : elements) {
        element.replaceWith(new TextNode(element.toString(),""));
    }
    return Jsoup.clean(doc.body().toString(), new Whitelist().addTags("a").addAttributes("a", "href", "name", "rel", "target"));
}

Jsoup - Howto clean html by escaping not deleting the unwanted html?

Document doc = Jsoup.parse("This is a <strong>strong</strong> number <date>2013</date>");

Spanned HtmlDoc = Html.fromHtml(doc.toString());
String fromHTML = HtmlDoc.toString();

System.out.println(fromHTML);

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