This is the page title

Jsoup clean HTML example

Jsoup clean HTML example shows how to clean HTML using Jsoup. The example also shows how to remove HTML tags from String and retain specific tags using a whitelist while cleaning the HTML using Jsoup.

How to remove HTML tags by cleaning the HTML using Jsoup?

You can remove HTML tags from String using the clean method of the Jsoup.

This method removes all HTML tags from the HTML string while retaining the tags included in the specified whitelist. By default, Jsoup provides the below-given whitelists out of the box.

1) none
All HTML tags are removed except for the text nodes.

2) simpleText
This whitelist allows only text formatting HTML tags b, em, i, strong and u. All other tags are removed.

3) basic
Basic whitelist allows a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul tags. All other tags are removed. It does not allow images.

4) basicWithImages
As the name suggests, this whitelist allows all tags included in the basic whitelist plus image (img tag).

5) relaxed
This is the most accommodating whitelist which allows a, b, blockquote, br, caption, cite, code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul tags.

Читайте также:  Как сделать константу в python

How to clean HTML using a whitelist?

Create an appropriate whitelist object and use it along with the clean method to clean the HTML and retain tags specified in the whitelist as given below.

Источник

Remove HTML Tags Using Java

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

Sometimes, we would like to remove all HTML tags and extract the text from an HTML document string.

The problem looks pretty straightforward. However, depending on the requirements, it can have different variants.

In this tutorial, we’ll discuss how to do that using Java.

2. Using Regex

Since we’ve already got the HTML as a String variable, we need to do a kind of text manipulation.

When facing text manipulation problems, regular expressions (Regex) could be the first idea coming up.

Removing HTML tags from a string won’t be a challenge for Regex since no matter the start or the end HTML elements, they follow the pattern “< … >”.

If we translate it into Regex, it would be “<[^>]*>” or “<.*?>”.

We should note that Regex does greedy matching by default. That is, the Regex “<.*>” won’t work for our problem since we want to match from ‘‘ until the next ‘>‘ instead of the last ‘>‘ in a line.

Now, let’s test if it can remove tags from an HTML source.

2.1. Removing Tags From example1.html

Before we test removing HTML tags, first let’s create an HTML example, say example1.html:

      

If the application X doesn't start, the possible causes could be:
1. Maven is not installed.
2. Not enough disk space.
3. Not enough memory.

Now, let’s write a test and use String.replaceAll() to remove HTML tags:

String html = . // load example1.html String result = html.replaceAll("<[^>]*>", ""); System.out.println(result); 

If we run the test method, we see the result:

 This is the page title If the application X doesn't start, the possible causes could be: 1. Maven is not installed. 2. Not enough disk space. 3. Not enough memory. 

The output looks pretty good. This is because all HTML tags have been removed.

It preserves whitespaces from the stripped HTML. But we can easily remove or skip those empty lines or whitespaces when we process the extracted text. So far, so good.

2.2. Removing Tags From example2.html

As we’ve just seen, using Regex to remove HTML tags is pretty straightforward. However, this approach may have problems since we cannot predict what HTML source we’ll get.

For example, an HTML document may have or tags, and we may not want to have their content in the result.

Further, the text in the , , or even the tags could contain “” or “>” characters. If this is the case, our Regex approach may fail.

Now, let’s see another HTML example, say example2.html:

       

If the application X doesn't start, the possible causes could be:
1. Maven is not installed.
2. Not enough ( <1G) disk space.
3. Not enough ( <64MB) memory.

This time, we have a tag and “” characters in the tag.

If we use the same method on example2.html, we’ll get (empty lines have been removed):

 This is the page title // some interesting script functions If the application X doesn't start, the possible causes could be: 1. Maven is not installed. 2. Not enough ( 3. Not enough (

Apparently, we’ve lost some text due to the “

Therefore, using Regex to process XML or HTML is fragile. Instead, we can choose an HTML parser to do the job.

Next, we’ll address a few easy-to-use HTML libraries to extract text.

3. Using Jsoup

Jsoup is a popular HTML parser. To extract text from an HTML document, we can simply call Jsoup.parse(htmlString).text().

First, we need to add the Jsoup library to the classpath. For example, let’s say we’re using Maven to manage project dependencies:

Now, let’s test it with our example2.html:

String html = . // load example2.html System.out.println(Jsoup.parse(html).text()); 

If we give the method a run, it prints:

This is the page title If the application X doesn't start, the possible causes could be: 1. Maven is not installed. 2. Not enough (<1G) disk space. 3. Not enough (<64MB) memory. 

As the output shows, Jsoup has successfully extracted texts from the HTML document. Also, the text in the element has been ignored.

Additionally, by default, Jsoup will remove all text formatting and whitespaces, such as line breaks.

However, if it's required, we can also ask Jsoup to preserve the line breaks.

4. Using HTMLCleaner

HTMLCleaner is another HTML parser. Its goal is to make “ill-formed and dirty” HTML from the Web suitable for further processing.

 net.sourceforge.htmlcleaner htmlcleaner 2.25  

We can set various options to control HTMLCleaner's parsing behavior.

Here, as an example, let's tell HTMLCleaner to skip the element when parsing example2.html:

String html = . // load example2.html CleanerProperties props = new CleanerProperties(); props.setPruneTags("script"); String result = new HtmlCleaner(props).clean(html).getText().toString(); System.out.println(result); 

HTMLCleaner will produce this output if we run the test:

 This is the page title If the application X doesn't start, the possible causes could be: 1. Maven is not installed. 2. Not enough (<1G) disk space. 3. Not enough (<64MB) memory. 

As we can see, the content in the element has been ignored.

Also, it converts
tags into line breaks in the extracted text
. This can be helpful if the format is significant.

On the other hand, HTMLCleaner preserves whitespace from the stripped HTML source. So, for example, the text “1. Maven is not installed” is broken into three lines.

5. Using Jericho

At last, we'll see another HTML parser – Jericho. It has a nice feature: rendering HTML markup with simple text formatting. We'll see it in action later.

As usual, let's first add the Jericho dependency in the pom.xml:

 net.htmlparser.jericho jericho-html 3.4  

In our example2.html, we have a hyperlink “Maven (http://maven.apache.org/)“. Now, let's say we would like to have both the link URL and link text in the result.

To do that, we can create a Renderer object and use the includeHyperlinkURLs option:

String html = . // load example2.html Source htmlSource = new Source(html); Segment segment = new Segment(htmlSource, 0, htmlSource.length()); Renderer htmlRender = new Renderer(segment).setIncludeHyperlinkURLs(true); System.out.println(htmlRender); 

Next, let's execute the test and check the output:

If the application X doesn't start, the possible causes could be: 1. Maven is not installed. 2. Not enough (<1G) disk space. 3. Not enough (<64MB) memory.

As we can see in the result above, the text has been pretty-formatted. Also, the text in the element is ignored by default.

The link URL is included as well. Apart from rendering links ( ), Jericho supports rendering other HTML tags, for example


,
,
bullet-list ( and ), and so on
.

6. Conclusion

In this article, we've addressed different ways to remove HTML tags and extract HTML text.

We should note that it's not a good practice to use Regex to process XML/HTML.

As always, the complete source code for this article can be found over on GitHub.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you'll have results within minutes:

Источник

Оцените статью