Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3522

Unescape HTML Symbols in Java

$
0
0
start here featured

1. Overview

Server-side applications sometimes require parsing of HTML characters. This is where the process of escaping/unescaping comes in helpful. In this tutorial, we’ll demonstrate a couple of ways to unescape HTML characters in Java. We’ll look into some of the available libraries that can handle this task.

2. Ways to Unescape HTML Character

Processing HTML symbols within the JVM can be tricky because these symbols in Java strings represent more than two or more characters. For example, < or > characters in HTML are represented as &lt; and &gt; Strings in Java. For the JVM to interpret these characters correctly, we need ways to escape or unescape them.

We can always write code ourselves to handle unescaping HTML characters, but the process would take time. It is also prone to errors. Instead, we can work with available Java libraries that can handle the task. The sample code in the subsections below tests the conversion of escaped HTML symbols to “unescaped” characters using various Java libraries.

2.1. Unescaping via Apache Commons’ StringEscapeUtils

Apache Commons is a popular Java library with the goal and focus on reusing components. Its StringEscapeUtils class has many convenient methods and among them is StringEscapeUtils.unescapeHtml4() within the package org.apache.commons.text:

String expectedQuote = "\"Hello\" Baeldung";
String escapedQuote = "&quot;Hello&quot; Baeldung";
Assert.assertEquals(expectedQuote, StringEscapeUtils.unescapeHtml4(escapedQuote));
String escapedStringsWithHtmlSymbol = "&lt;p&gt;&lt;strong&gt;Test sentence in bold type.&lt;/strong&gt;&lt;/p&gt;";
String expectedStringsWithHtmlSymbol = "<p><strong>Test sentence in bold type.</strong></p>";
Assert.assertEquals(expectedStringsWithHtmlSymbol, StringEscapeUtils.unescapeHtml4(escapedStringsWithHtmlSymbol));

2.2. Unescaping via Spring Framework’s HtmlUtils

Spring Framework is a solid Java platform that has various infrastructure support for developing applications. It provides the HtmlUtils.htmlUnescape() function which is used to convert escaped HTML characters. It can be found under the package org.springframework.web.util:

String expectedQuote = "\"Code smells\" -Martin Fowler";
String escapedQuote = "&quot;Code smells&quot; -Martin Fowler";
Assert.assertEquals(expectedQuote, HtmlUtils.htmlUnescape(escapedQuote));
String escapedStringsWithHtmlSymbol = "&lt;p&gt;Loren Ipsum is a popular paragraph.&lt;/p&gt;";
String expectedStringsWithHtmlSymbol = "<p>Loren Ipsum is a popular paragraph.</p>";
Assert.assertEquals(expectedStringsWithHtmlSymbol, HtmlUtils.htmlUnescape(escapedStringsWithHtmlSymbol));

2.3. Unescaping via Unbescape’s HtmlEscape

The Unbescape library is for escaping and unescaping many formats of data, among them HTML, JSON, and CSS. The example below shows unescaping HTML characters and tags via the HtmlEscape.unescapeHtml() method:

String expectedQuote = "\"Carpe diem\" -Horace";
String escapedQuote = "&quot;Carpe diem&quot; -Horace";
Assert.assertEquals(expectedQuote, HtmlEscape.unescapeHtml(escapedQuote));
String escapedStringsWithHtmlSymbol = "&lt;p&gt;&lt;em&gt;Pizza is a famous Italian food. Duh.&lt;/em&gt;&lt;/p&gt;";
String expectedStringsWithHtmlSymbol = "<p><em>Pizza is a famous Italian food. Duh.</em></p>";
Assert.assertEquals(expectedStringsWithHtmlSymbol, HtmlEscape.unescapeHtml(escapedStringsWithHtmlSymbol));

2.4. Unescaping via Jsoup’s Entities.unescape()

The Jsoup library is for all sorts of HTML manipulation. Its Java HTML Parser offers a wide range of support for HTML and XML requirements. Entities.unescape() is a function whose main goal is to unescape Strings with escaped HTML characters:

String expectedQuote = "\"Jsoup\" is another strong library";
String escapedQuote = "&quot;Jsoup&quot; is another strong library";
Assert.assertEquals(expectedQuote,  Entities.unescape(escapedQuote));
String escapedStringsWithHtmlSymbol = "&lt;p&gt;It simplifies working with real-world &lt;strong&gt;HTML&lt;/strong&gt; and &lt;strong&gt;XML&lt;/strong&gt;&lt;/p&gt;";
String expectedStringsWithHtmlSymbol = "<p>It simplifies working with real-world <strong>HTML</strong> and <strong>XML</strong></p>";
Assert.assertEquals(expectedStringsWithHtmlSymbol,  Entities.unescape(escapedStringsWithHtmlSymbol));

 3. Conclusion

In this tutorial, we’ve demonstrated ways how to unescape HTML characters by using various libraries available to the Java community. Libraries like Apache Commons and Spring Framework are popular libraries that offer various tools for processing HTML entities. Unbescape and Jsoup offer processing capabilities not just for HTML characters, but also other forms of data formats. They help validate inputs from the server side whenever an application requires it.

All code samples used in the article are available over on GitHub.

       

Viewing all articles
Browse latest Browse all 3522

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>