Convert HTML to text : HTML Output « Servlets « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Servlets » HTML OutputScreenshots 
Convert HTML to text
   
/*
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can get a copy of the License at http://www.thinkingrock.com.au/cddl.html
 * or http://www.thinkingrock.com.au/cddl.txt.
 *
 * When distributing Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://www.thinkingrock.com.au/cddl.txt.
 * If applicable, add the following below the CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * The Original Software is ThinkingRock. The Initial Developer of the Original
 * Software is Avente Pty Ltd, Australia.
 *
 * Portions Copyright 2006-2007 Avente Pty Ltd. All Rights Reserved.
 */

import java.awt.Color;
import java.io.Reader;
import java.io.StringReader;
import java.util.logging.Logger;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLEditorKit;

/**
 * Some static utility methods for HTML stuff.
 *
 @author Jeremy Moore
 */
public final class HTML {
    
    /**
     * Utility method to format a color to HTML RGB color format (e.g. #FF0000 for Color.red).
     @param color The color.
     @return the HTML RGB color string.
     */
    public static final String format(Color c) {
        String r = (c.getRed() 16"0" + Integer.toHexString(c.getRed()) : Integer.toHexString(c.getRed());
        String g = (c.getGreen() 16"0" + Integer.toHexString(c.getGreen()) : Integer.toHexString(c.getGreen());
        String b = (c.getBlue() 16"0" + Integer.toHexString(c.getBlue()) : Integer.toHexString(c.getBlue());
        return "#" + r + g + b;
    }
    
    /**
     * Utility method to convert HTML to text.
     @param html The string containing HTML.
     @return a String containing the derived text .
     */
    public static final String html2text(String html) {
        EditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
        try {
            Reader reader = new StringReader(html);
            kit.read(reader, doc, 0);
            return doc.getText(0, doc.getLength());
        catch (Exception e) {
          
            return "";
        }
    }
    
}

   
    
    
  
Related examples in the same category
1. Servlet Output HTML Demo
2. Servlet Display Static HTML
3. Prints a conversion table of miles per gallon to kilometers per liter
4. Servlet: Print Table
5. Html utilities
6. Html Parse Servlet
7. Escape and unescape string
8. Escapes newlines, tabs, backslashes, and quotes in the specified string
9. Web Calendar
10. HTML Helper
11. Escape HTML
12. Text To HTML
13. Unescape HTML
14. Java object representations of the HTML table structure
15. Entity Decoder
16. Format a color to HTML RGB color format (e.g. #FF0000 for Color.red)
17. Definitions of HTML character entities and conversions between unicode characters and HTML character entities
18. Encode special characters and do formatting for HTML output
19. HTML color names
20. Utility methods for dealing with HTML
21. Filter the specified message string for characters that are sensitive in HTML
22. A collection of all character entites defined in the HTML4 standard.
23. Decode an HTML color string like '#F567BA;' into a Color
24. Normalize Post Data
25. Get HTML Color String from Java Color object
26. HTML Decoder
27. HTML Parser
28. HTML color and Java Color
29. HTML form Utilites
30. Html Dimensions
31. break Lines with HTML
32. insert HTML block dynamically
33. Convert an integer to an HTML RGB value
34. Convert to HTML string
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.