Convert HTML to Plain Text in Java

Learn how to convert HTML to plain text in Java using built-in parsing, Jsoup, or a simple tag-stripping approach. Includes practical steps, output checks, and common corrections.

Before you start

The process runs locally in your Java application. You do not need a web service or an internet connection for the basic conversion. Decide which approach fits your input: a simple regular-expression cleanup can work for short, controlled snippets, while a real HTML parser such as Jsoup is more reliable for pages with entities, scripts, styles, comments, or unusual nesting.

For Jsoup, add the library to your project. In Maven, declare the Jsoup dependency in the dependencies section of the project descriptor. In Gradle, add the equivalent implementation dependency. Check the library version against your Java version if compatibility matters for your build.

Step-by-step instructions

Option 1: Convert with Jsoup

  • Create a string containing the HTML source you want to convert.
  • Parse the string with the HTML parser method that accepts a string input.
  • Call the text extraction method on the parsed document.
  • Use the returned string as your plain text result.

Jsoup removes tags, decodes common character entities, and drops script and style content from the text result. Whitespace may still reflect the original markup, so trim or normalize spacing if your use requires compact text.

Option 2: Use a built-in parser

If you cannot add a third-party dependency, you can parse the HTML with the standard HTML parser available in your Java version. The default parser is lenient and accepts many real-world documents that are not strictly valid HTML. Obtain the root element, then walk the document tree and collect text nodes in document order. This gives you more control over which elements to skip.

  • Create a document builder and disable external entity loading if untrusted input may include a document type declaration.
  • Parse the HTML from a string or input stream.
  • Find the body element when available, otherwise use the document element.
  • Traverse descendant nodes and append text-node values to a result buffer.
  • Convert the final buffer to a string.

The built-in parser may not decode all HTML entities the same way as a browser, and its handling of malformed markup depends on the Java version. It is a reasonable fallback for basic markup and local processing.

Option 3: Strip tags without a parser

For very small, trusted snippets, you can remove comments, script blocks, and style blocks, then remove all remaining angle-bracket tags. Decode common named and numeric entities as a final cleanup step.

  • Remove HTML comments.
  • Remove the content between opening and closing script and style tags.
  • Replace block-level closing tags with a space or newline where you want separation.
  • Remove all remaining tags.
  • Decode the character entities you expect in the input.

This approach is compact, but it is not a substitute for a parser when content contains unusual attributes, embedded quotes, conditional comments, or malformed tags.

Check the output

Run the conversion on a small fixture that contains a heading, a paragraph, a link, an ampersand, and a script tag. The result should include the visible heading and paragraph text, show the decoded ampersand, and omit the script contents.

Check that reading order matches the source. Plain text has no bold, links, images, or heading hierarchy, so verify that any text separated only by styling remains readable when the formatting disappears.

Common problems and next steps

If output contains script or style text, make sure the previous tag-stripping pass is not running before those blocks are removed. With a parser, confirm that you are extracting text content rather than serializing the whole element.

If words run together, add a space or newline after block-level elements before collecting text. If entities remain visible, apply entity decoding after tag removal rather than before. If a Java version changes the built-in parser behavior, test the same fixture before using that approach in production.

For frequent conversion of untrusted or large HTML, prefer a maintained HTML parser and set a maximum input length before parsing. When you need friendlier formatting, transform block elements into paragraph breaks or list markers before extracting text.