Take the 2-minute tour ×

How may I convert HTML to text keeping linebreaks (produced by elements like br,p,div, ...) possibly using NekoHTML or any decent enough HTML parser


share|improve this question
go ahead, just do it! Btw, what is your question? –  lexu Mar 25 '10 at 7:28
I think the question is one of rendering as Neko appears to be only a parser. For example a <p> element is word-wrapped by an HTML renderer but not by a parser. If this guess is correct, the post is underspecified as to what the output should be rendered as. –  msw Mar 25 '10 at 7:38
There's another question discussing your problem, and I've found propsed by @Mirco Attocchi pretty amazing and it works great for me. stackoverflow.com/a/6031463/1249304 –  Muhammad Sabry Nov 12 '12 at 17:07
add comment

6 Answers

Here is a function I made to output text (including line breaks) by iterating over the nodes using Jsoup.

public static String htmlToText(InputStream html) throws IOException {
    Document document = Jsoup.parse(html, null, "");
    Element body = document.body();

    return buildStringFromNode(body).toString();

private static StringBuffer buildStringFromNode(Node node) {
    StringBuffer buffer = new StringBuffer();

    if (node instanceof TextNode) {
        TextNode textNode = (TextNode) node;

    for (Node childNode : node.childNodes()) {

    if (node instanceof Element) {
        Element element = (Element) node;
        String tagName = element.tagName();
        if ("p".equals(tagName) || "br".equals(tagName)) {

    return buffer;
share|improve this answer
I've added the following to make it include links: if ("a".equals(tagName)){buffer.append(node.attr("href")); buffer.append("\n"); } –  Jan Vladimir Mostert Sep 20 '13 at 6:07
add comment
w3m -dump -no-cookie input.html > output.txt
share|improve this answer
add comment

If you don't mind hard-wrapped/designed-for-monospace output, lynx -dump produces good plain text from HTML.

share|improve this answer
add comment

I did find a relatively clever solution in html2txt: THE ASCIINATOR which does an admirable job of producing nroff like output (e.g. like man ls run on a terminal). It produces output in the Markdown style that StackOverflow uses as input.

For moderately complex pages like this page, the output is somewhat scattered as it tries mightily to turn non-linear layout into something linear. The output from less complicated markup is pretty readable.

share|improve this answer
add comment

HTML to Text: I am taking this statement to mean that all HTML formatting, except line-breaks, will be abandoned.

What I have done for such a venture is using regexp to detect any set of tag enclosure. If the value within the tags are br or br/, a line-break is inserted, otherwise the tag is discarded.

It works only for simple html pages. Tables will obviously be linearised.

I had been thinking of detecting the title value between the title tag enclosure, so that the converter automatically places the title at the top of the page. Needs to put in a little more algorithm. By my time is better spent with ...

I am reading on using Google Data APIs to upload a document to Google Docs and then using the same API to download/export it as text. Or, why text, when I could do pdf. But you have to get a Google account if you don't already have one.

Google docs data download/export

Google docs data api for java

share|improve this answer
I cannot use external services. The application should even run offline. –  Eduardo May 29 '10 at 10:19
add comment

Does it matter what language you use? You could always use pattern matching. Basically HTML lien break tags (br,p,div, ...) you can replace with "\n" and remove all the other tags. You could always store the tags in an array so you can easily check when you go through the HTML file. Then any other tags and all the other end tags (/p,..) can be replaced with an empty string therefore getting your result.

share|improve this answer
That is one thing a HTML parser like NekoHTML does, but it is not enough since you need other things like entity replacement. –  Eduardo May 29 '10 at 10:18
add comment

Your Answer


