title: Convert HTML to plain text in Java
body: <p>I need to convert HTML to plain text. My only requirement of formatting is to retain new lines in the plain text. New lines should be displayed not only in the case of <code>&lt;br&gt;</code> but other tags, e.g. <code>&lt;tr/&gt;</code>, <code>&lt;/p&gt;</code> leads to a new line too. </p>
  
  <p>Sample HTML pages for testing are: </p>
  
  <ul>
  <li><a href="http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/scannerConsole.html" rel="nofollow noreferrer">http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/scannerConsole.html</a></li>
  <li><a href="http://www.javadb.com/write-to-file-using-bufferedwriter" rel="nofollow noreferrer">http://www.javadb.com/write-to-file-using-bufferedwriter</a></li>
  </ul>
  
  <p>Note that these are only random URLs.</p>
  
  <p>I have tried out various libraries (JSoup, Javax.swing, Apache utils) mentioned in the answers to <a href="http://stackoverflow.com/questions/240546/removing-html-from-a-java-string">this StackOverflow question</a> to convert HTML to plain text. </p>
  
  <p>Example using JSoup:</p>
  
  <pre><code>public class JSoupTest {
  
   @Test
   public void SimpleParse() {
    try {
     Document doc = Jsoup.connect("http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/scannerConsole.html").get();
     System.out.print(doc.text());
  
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  </code></pre>
  
  <p>Example with HTMLEditorKit: </p>
  
  <pre><code>import javax.swing.text.html.*;
  import javax.swing.text.html.parser.*;
  
  public class Html2Text extends HTMLEditorKit.ParserCallback {
   StringBuffer s;
  
   public Html2Text() {}
  
   public void parse(Reader in) throws IOException {
     s = new StringBuffer();
     ParserDelegator delegator = new ParserDelegator();
     // the third parameter is TRUE to ignore charset directive
     delegator.parse(in, this, Boolean.TRUE);
   }
  
   public void handleText(char[] text, int pos) {
     s.append(text);
   }
  
   public String getText() {
     return s.toString();
   }
  
   public static void main (String[] args) {
     try {
       // the HTML to convert
      URL  url = new URL("http://www.javadb.com/write-to-file-using-bufferedwriter");
      URLConnection conn = url.openConnection();
      BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
      String inputLine;
      String finalContents = "";
      while ((inputLine = reader.readLine()) != null) {
       finalContents += "\n" + inputLine.replace("&lt;br", "\n&lt;br");
      }
      BufferedWriter writer = new BufferedWriter(new FileWriter("samples/testHtml.html"));
      writer.write(finalContents);
      writer.close();
  
       FileReader in = new FileReader("samples/testHtml.html");
       Html2Text parser = new Html2Text();
       parser.parse(in);
       in.close();
       System.out.println(parser.getText());
     }
     catch (Exception e) {
       e.printStackTrace();
     }
   }
  }
  </code></pre>
  
  <p><br/></p>