How to Convert HTML Strings to Text using Java

Cloudmersive
2 min readJun 10, 2022

--

It’s tough to edit web copy when it’s wrapped up in layers of HTML syntax. Converting HTML strings to Text is a great way to strip away the syntax and get directly after the content it contains. Our HTML String to Text API provides a fully integrated method to pull that off, and it’s easy to take advantage of this service for you website or application using the code examples we’ve provided below. These examples are specifically for structuring your API call in Java, but you can also find comparable examples available in more than a dozen additional programming languages on our API console page.

For starters, let’s install maven by first adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, let’s include a reference in the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now let’s copy in the imports:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertWebApi;

Finally, it’s time to get our two parameters ready including:

  1. Your HTML text string input.
  2. Your Cloudmersive API key (get one by visiting our website and registering a free account).

Once those are ready, include them in the remaining code block to complete the API call:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertWebApi apiInstance = new ConvertWebApi();
HtmlToTextRequest input = new HtmlToTextRequest(); // HtmlToTextRequest | HTML to Text request parameters
try {
HtmlToTextResponse result = apiInstance.convertWebHtmlToTxt_0(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertWebApi#convertWebHtmlToTxt_0");
e.printStackTrace();
}

Just like that, you’re all done. No further code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet