title: Remove all HTML with Jsoup but keep the lines
body: <p>I have a <code>String</code> which contains some of the content of an e-mail, I want to remove all the HTML coding from this <code>String</code>.</p>
  
  <p>This is my code at the moment:</p>
  
  <pre><code>public static String html2text(String html) {
  
     Document document = Jsoup.parse(html);
     document = new Cleaner(Whitelist.basic()).clean(document);
     document.outputSettings().escapeMode(EscapeMode.xhtml);
     document.outputSettings().charset("UTF-8");
     html = document.body().html();
  
     html = html.replaceAll("&lt;br /&gt;", "");
  
     splittedStr = html.split("Geachte heer/mevrouw,");
  
     html = splittedStr[1];
  
     html = "Geachte heer/mevrouw,"+html;
  
     return html;
  }
  </code></pre>
  
  <p>This method removes all HTML, keeps lines and most of the layout. But it will also return some <code>&amp;amp;</code> and <code>nbsp;</code> tags, which aren't completely removed. See the output below, as you can see there are still some tags and even parts of it in the <code>String</code>. How do I get rid of these?</p>
  
  <pre><code>  Loonheffingen       &amp;amp;n= bsp; Naam
   nr         in administratie         &amp;amp;nbs= p;           meldingen
    nummer
  
   1          &amp;amp;n= bsp;            = ;     0            &amp;amp;= nbsp;           &amp;amp;nbs= p;           1
             123456789L01
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>&lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;De afgekeurde meldingen zijn opgenomen in de bijlage: Afgekeurde meldingen.&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;Wilt u zo spoedig mogelijk zorgdragen dat deze&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;meldingen gecorrigeerd worden aangeleverd?&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;mer&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;Volg &amp;nbsp; &amp;nbsp; Aantal verwerkt &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Aantal afgekeurde&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;&amp;nbsp;Loonheffingen &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Naam&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;nr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; in administratie &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; meldingen&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;&amp;nbsp;nummer&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  &lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;&lt;span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/span&gt;&lt;br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"&gt;
  </code></pre>
  
  <p>This is a part of the HTML I am trying to parse. I want to remove all the HTML, but keep the layout of the original e-mail.</p>
  
  <p>Any help is appreciated, </p>
  
  <p>Thanks!</p>
  
  <p><strong>Solved</strong></p>
  
  <pre><code>        Document xmlDoc = Jsoup.parse(file, "", Parser.xmlParser());
          Elements spans= xmlDoc.select("span");
  
          for (Element link : spans) {
              String html = textPlus(link);
              System.out.println(html);
          }
  
  
   public static String textPlus(Element elem) {
      List&lt;TextNode&gt; textNodes = elem.textNodes();
      if (textNodes.isEmpty()) {
          return "";
      }
  
      StringBuilder result = new StringBuilder();
      // start at the first text node
      Node currentNode = textNodes.get(0);
      while (currentNode != null) {
          // append deep text of all subsequent nodes
          if (currentNode instanceof TextNode) {
              TextNode currentText = (TextNode) currentNode;
              result.append(currentText.text());
          } else if (currentNode instanceof Element) {
              Element currentElement = (Element) currentNode;
              result.append(currentElement.text());
          }
          currentNode = currentNode.nextSibling();
      }
      return result.toString();
  }
  </code></pre>
  
  <p>Code was provided as an answer on <a href="https://stackoverflow.com/questions/13042367/get-the-text-in-anchors-within-text-nodes">this</a> question.</p>