friend's I have to parse the description from url,where parsed content have few html tags,so how can I convert it to plain text.
-
What are your precise requirements? Do you need to strip HTML tags? Extract the content of a specific tag?Vivien Barousse– Vivien Barousse2010-08-31 10:05:18 +00:00Commented Aug 31, 2010 at 10:05
-
i can able to extract the content,but the content have <p>zcc dsdfsf ddfdfsf </P><span>sfdfdfdfdf</span>, like the above i'm getting my data but i need to be a simple plain text.without those html tagsMGSenthil– MGSenthil2010-08-31 10:54:37 +00:00Commented Aug 31, 2010 at 10:54
-
Similar question with good answer here : stackoverflow.com/questions/1518675/…. I used Jericho and it works fine.рüффп– рüффп2013-09-03 09:49:43 +00:00Commented Sep 3, 2013 at 9:49
-
1You should mark this question as answered.ankitjaininfo– ankitjaininfo2014-03-26 06:30:07 +00:00Commented Mar 26, 2014 at 6:30
-
1Duplicate of stackoverflow.com/q/240546/873282, stackoverflow.com/q/1699313/873282, stackoverflow.com/q/1518675/873282, and stackoverflow.com/q/832620/873282koppor– koppor2016-12-11 21:45:09 +00:00Commented Dec 11, 2016 at 21:45
10 Answers
Yes, Jsoup will be the better option. Just do like below to convert the whole HTML text to plain text.
String plainText= Jsoup.parse(yout_html_text).text();
2 Comments
Jsoup.parse(html).wholeText()Just getting rid of HTML tags is simple:
// replace all occurrences of one or more HTML tags with optional
// whitespace inbetween with a single space character
String strippedText = htmlText.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", " ");
But unfortunately the requirements are never that simple:
Usually, <p> and <div> elements need a separate handling, there may be cdata blocks with > characters (e.g. javascript) that mess up the regex etc.
2 Comments
Use Jsoup.
Add the dependency
<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
Now in your java code:
public static String html2text(String html) {
return Jsoup.parse(html).wholeText();
}
Just call the method html2text with passing the html text and it will return plain text.
Comments
If you want to parse like browser display, use:
import net.htmlparser.jericho.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class RenderToText {
public static void main(String[] args) throws Exception {
String sourceUrlString="data/test.html";
if (args.length==0)
System.err.println("Using default argument of \""+sourceUrlString+'"');
else
sourceUrlString=args[0];
if (sourceUrlString.indexOf(':')==-1) sourceUrlString="file:"+sourceUrlString;
Source source=new Source(new URL(sourceUrlString));
String renderedText=source.getRenderer().toString();
System.out.println("\nSimple rendering of the HTML document:\n");
System.out.println(renderedText);
}
}
I hope this will help to parse table also in the browser format.
Thanks, Ganesh
1 Comment
I'd recommend parsing the raw HTML through jTidy which should give you output which you can write xpath expressions against. This is the most robust way I've found of scraping HTML.
Comments
I needed a plain text representation of some HTML which included FreeMarker tags. The problem was handed to me with a JSoup solution, but JSoup was escaping the FreeMarker tags, thus breaking the functionality. I also tried htmlCleaner (sourceforge), but that left the HTML header and style content (tags removed). http://stackoverflow.com/questions/1518675/open-source-java-library-for-html-to-text-conversion/1519726#1519726
My code:
return new net.htmlparser.jericho.Source(html).getRenderer().setMaxLineLength(Integer.MAX_VALUE).setNewLine(null).toString();
The maxLineLength ensures lines are not artificially wrapped at 80 characters.
The setNewLine(null) uses the same new line character(s) as the source.
Comments
Using Jsoup, I got all the text in the same line.
So I used the following block of code to parse HTML and keep new lines:
private String parseHTMLContent(String toString) {
String result = toString.replaceAll("\\<.*?\\>", "\n");
String previousResult = "";
while(!previousResult.equals(result)){
previousResult = result;
result = result.replaceAll("\n\n","\n");
}
return result;
}
Not the best solution but solved my problem :)