java html串转换成文本串

阅读更多
采用htmlparser 来解决将html串中抽取出文本串。


String str = "" +
"" +
"" +
" "href=\"BLOCKQUOTE{margin-Top: 0px; margin-Bottom: 0px; margin-Left: 2em}\"" +
"rel=stylesheet>" +
"" +
"
helll,测试邮件
" +
"
 
" +
"
2011-03-03 " +
"
"+
"
"+

"
shopeye7 " +
"
" ;

System.out.println(StringUtil.html2Str(str));

效果:
helll,测试邮件 2011-03-03 shopeye7


方法:
/**
* @param html
* @return
*/
public static String html2Str(String html) {
try {
html = nvl(html);
Parser parser = Parser.createParser(html, "utf-8");
TextExtractingVisitor visitor = new TextExtractingVisitor();
parser.visitAllNodesWith(visitor);
return visitor.getExtractedText();
} catch (Exception ex) {
return null;
}
}
  • lib.rar (300.7 KB)
  • 下载次数: 203

你可能感兴趣的:(HTML,Java)