1、做门户网站需要在首页展示文章的摘要部分,数据库存储的是带标签的内容,展示在前台需要将html标签处理一下

2、解决方式:

一、replaceAll  与正则表达式



//从html中提取纯文本  
public static String StripHT(String strHtml) {
//剔出<html>的标签
String txtcontent = strHtml.replaceAll("</?[^>]+>", "");
//去除字符串中的空格,回车,换行符,制表符
txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");
return txtcontent;
}


二:正则表达式



//从html中提取纯文本
public static String Html2Text(String inputString) {
// 含html标签的字符串
String htmlStr = inputString;
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 {
// 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
// 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
// 定义HTML标签的正则表达式
String regEx_html = "<[^>]+>";
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
// 过滤script标签
htmlStr = m_script.replaceAll("");
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
// 过滤style标签
htmlStr = m_style.replaceAll("");
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
// 过滤html标签
htmlStr = m_html.replaceAll("");
textStr = htmlStr;
} catch (Exception e) {System.err.println("Html2Text: " + e.getMessage()); }
//剔除空格行
textStr=textStr.replaceAll("[ ]+", " ");
textStr=textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
// 返回文本字符串
return textStr;
}


三:HTMLEditorKit.ParserCallback,Java自带的类



public class HtmlToText extends HTMLEditorKit.ParserCallback {

StringBuffer s;

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);
}

@Override
public void handleText(char[] text, int pos) {
s.append(text);
}

public String getText() {
return s.toString();
}

public static void main(String[] args) {

String str = "<div id=\"xw_box\"> \n" +
" <p class=\"mt20 yahei fz12\" style=\"font-size: 14px; margin-top: -28px; text-align: right;\"><em>字号:\n"+
          " <a onclick=\"changeSize('16px');return false;\" href=\"#\">大</a> <a onclick=\"changeSize('14px');return false;\" href=\"#\">中</a></em></p> \n" +
" <p style=\"text-indent: 2em;\"></p> \n" +
" <p style=\"text-indent: 2em;\">Java是一门面向对象编程语言,\n" +
          " 不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、\n" +
          " 指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,\n" +
          " 极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程</p> \n" +
" <p style=\"text-indent: 2em;\">20世纪90年代,硬件领域出现了单片式计算机系统,\n" +
         " 这种价格低廉的系统一出现就立即引起了自动控制领域人员的注意,因为使用它可以大幅度提升消费类电子产品\n" +
         " (如电视机顶盒、面包烤箱、移动电话等)的智能化程度。Sun公司为了抢占市场先机,在1991年成立了一个称为Green的项目小组,\n" +
         " 帕特里克、詹姆斯·高斯林、麦克·舍林丹和其他几个工程师一起组成的工作小组在加利福尼亚州门洛帕克市沙丘路的一个小工作室里面研究开发新技术,\n" +
          " 专攻计算机在家电产品上的嵌入式应用。</p> \n" +
" <p style=\"text-indent: 2em;\"></p> \n" +
"</div>";

//输入流
InputStream myIn=new ByteArrayInputStream(str.getBytes());
//将System.in转化为面向字符的流
InputStreamReader ir = new InputStreamReader(myIn);
try {
// the HTML to convert
//Reader in=new StringReader("string");
HtmlToText parser = new HtmlToText();
parser.parse(ir);
ir.close();
System.out.println(parser.getText());
} catch (Exception e) {
e.printStackTrace();
}
}

}