
“超文本”就是指页面内可以包含图片、链接,甚至音乐、程序等非文字元素。
超文本标记语言的结构包括“头”部分(英语:Head)、和“主体”部分(英语:Body),其中“头”部提供关于网页的信息,“主体”部分提供网页的具体内容。
1、在做接口的时候,有些模板xml标签不是CDATA规范的,这样就会在碰到&这样的字符出现错误,需要替换,简单的思路是字符串替换或正则匹配,将&替换成&。
2、特别是有些专辑描述是html的内容,写入xml文件的时候需要将html内容转化为文本内容。
3、下面给出HTML内容输出为存文本的两种工具方法,方便以后查阅。
方法一:正则替换:
/**
* html转化为text
* @param inputString
* @return
*/
public static String html2Text(String inputString) {
String htmlStr = inputString// 含html标签的字符串
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]*?>"// 定义script的正则表达式{或<script>]*?>[\s\S]*?<\/script>
// }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"// 定义style的正则表达式{或<style>]*?>[\s\S]*?<\/style>
// }
String regEx_html = "<[^>]+>"// 定义HTML标签的正则表达式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE)
m_script = p_script.matcher(htmlStr)
htmlStr = m_script.replaceAll("")// 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE)
m_style = p_style.matcher(htmlStr)
htmlStr = m_style.replaceAll("")// 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE)
m_html = p_html.matcher(htmlStr)
htmlStr = m_html.replaceAll("")// 过滤html标签
textStr = htmlStr
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage())
}
return textStr
}
方法二:采用api形式,先倒入jar包htmllexer.jar,htmlparser.jar,sitecapturer.jar,thumbelina.jar,filterbuilder.jar :
public static String getHtmlText(String htmlContent) throws Exception
{
if(htmlContent==null)htmlContent=""
// 增加一个<br/>,经测试,如果正文为纯文本,org.htmlparser会把参数当作一个文件处理
StringBuffer sbf = new StringBuffer("")
sbf.append("<br />").append(htmlContent)
Parser parser = new Parser(sbf.toString())
TextExtractingVisitor visitor = new TextExtractingVisitor()
parser.visitAllNodesWith(visitor)
String sReturn = visitor.getExtractedText()
sReturn = sReturn.replace(" ", "")//去掉空格以便统计字数
return sReturn
}
可以在想要显示的代码的外面加上<xmp></xmp>,这样<xmp>标签里面的内容将原封不动的显示出来。例如(Html代码)
<xmp>
<table>
<tr>
<th width="120">a</td>
<th width="120">b</td>
<th width="120">c</td>
</tr>
</table>
</xmp>
不知道这个用的着不!在网页刚流行起来的时候,提取html中的文本有一个简单的方法,就是将html文本(包含标记)中的所有以“<”符号开头到以“>”符号之间的内容去掉即可。
但对于现在复杂的网页而言,用这种方法提取出来的文本会有大量的空格、空行、script段落、还有一些html转义字符,效果很差。
下面用正则表达式来提取html中的文本,
代码的实现的思路是:
a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
b、将<head>标记中的所有内容去掉
c、将<script>标记中的所有内容去掉
d、将<style>标记中的所有内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉所有以“<>”符号为头尾的标记去掉。
g、转换&,&nbps等转义字符换成相应的符号
h、去掉多余的空格和空行
代码如下:
using System
using System.Text.RegularExpressions
namespace Kwanhong.Utilities
{
/// <summary>
/// HtmlToText 的摘要说明。
/// </summary>
public class HtmlToText
{
public string Convert(string source)
{
string result
//remove line breaks,tabs
result = source.Replace("\r", " ")
result = result.Replace("\n", " ")
result = result.Replace("\t", " ")
//remove the header
result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase)
//remove all styles
result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase)//clearing attributes
result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase)
//insert tabs in spaces of <td>tags
result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase)
//insert line breaks in places of <br>and <li>tags
result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase)
//insert line paragraphs in places of <tr>and <p>tags
result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase)
//remove anything thats enclosed inside <>
result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase)
//replace special characters:
result = Regex.Replace(result, @"&", "&", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<", "<", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @">", ">", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"&(.{2,6})", string.Empty, RegexOptions.IgnoreCase)
//remove extra line breaks and tabs
result = Regex.Replace(result, @" ( )+", " ")
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r")
result = Regex.Replace(result, @"(\r\r)+", "\r\n")
return result
}
}//end class
}//end namespace
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)