1. Application scenario: Extract plain text from an HTML file or from a String (which is HTML content), remove the page tag;
2. Code 1: replaceAll done
[java] view plain copy
- // Extract plain text from HTML
- public static String StripHT(String strHtml) {
- String txtcontent = strHtml.replaceAll(”
] + > “, “”); // Remove the < HTML > tag - txtcontent = txtcontent.replaceAll(“\\s*|\t|\r|\n“, “”); // Remove Spaces, carriage returns, newlines, and tabs from strings
- return txtcontent;
- }
Code 2: Regular expression done
[java] view plain copy
- // Extract plain text from HTML
- public static String Html2Text(String inputString) {
- String htmlStr = inputString; // A string containing HTML tags
- String textStr = “”;
- java.util.regex.Pattern p_script;
- java.util.regex.Matcher m_script;
- java.util.regex.Pattern p_style;
- java.util.regex.Matcher m_style;
- java.util.regex.Pattern p_html;
- java.util.regex.Matcher m_html;
- try {
- String regEx_script = “<[\\s]*? script[^>]*? >[\\s\\S]*? <[\\s]*? \/[\\s]*? script[\\s]*? > “; // Define a regular expression for script {or
[^> - String regEx_style = “<[\\s]*? style[^>]*? >[\\s\\S]*? <[\\s]*? \/[\\s]*? style[\\s]*? > “; // Define a regular expression for style {or
[^>
- String regEx_html = “<[^>]+>”; // Define regular expressions for HTML tags
- p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
- m_script = p_script.matcher(htmlStr);
- htmlStr = m_script.replaceAll(“”); // Filter script tags
- p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
- m_style = p_style.matcher(htmlStr);
- htmlStr = m_style.replaceAll(“”); // Filter the style tag
- p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
- m_html = p_html.matcher(htmlStr);
- htmlStr = m_html.replaceAll(“”); // Filter HTML tags
- textStr = htmlStr;
- } catch (Exception e) {System.err.println(“Html2Text: ” + e.getMessage()); }
- // Remove blank lines
- textStr=textStr.replaceAll(“[ ]+”, ” “);
- textStr=textStr.replaceAll(“(? m)^\\s*$(\\n|\\r\\n)”, “”);
- return textStr; // Returns a text string
- }
3, code 3: HTMLEditorKit ParserCallback fix, Java’s own class
[java] view plain copy
- package com.util;
- import java.io.*;
- 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
- //Reader in=new StringReader(“string”);
- FileReader in = new FileReader(“java-new.html”);
- Html2Text parser = new Html2Text();
- parser.parse(in);
- in.close();
- System.out.println(parser.getText());
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }