convert html to txt

Posted on 2004-11-02
Last Modified: 2010-03-31
input is a file in html format, and output will be a file in txt format
Any example will be approciated.
Thank you very much.
Question by:nphnhi
    LVL 37

    Accepted Solution

    LVL 14

    Assisted Solution

    // This method takes a URI which can be either a filename (e.g. file://c:/dir/file.html)
        // or a URL (e.g. and returns all text in the document.
        public static String getText(String uriStr) {
            final StringBuffer buf = new StringBuffer(1000);
            try {
                // Create an HTML document that appends all text to buf
                HTMLDocument doc = new HTMLDocument() {
                    public HTMLEditorKit.ParserCallback getReader(int pos) {
                        return new HTMLEditorKit.ParserCallback() {
                            // This method is whenever text is encountered in the HTML file
                            public void handleText(char[] data, int pos) {
                // Create a reader on the HTML content
                URL url = new URI(uriStr).toURL();
                URLConnection conn = url.openConnection();
                Reader rd = new InputStreamReader(conn.getInputStream());
                // Parse the HTML
                EditorKit kit = new HTMLEditorKit();
      , doc, 0);
            } catch (MalformedURLException e) {
            } catch (URISyntaxException e) {
            } catch (BadLocationException e) {
            } catch (IOException e) {
            // Return the text
            return buf.toString();
    LVL 14

    Expert Comment

    LVL 14

    Expert Comment

    thanks :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now