1.整体分析

1.1.首先看一下源代码,可以直接Copy。

public class HtmlUtil {

    /**
* 获取 html 中的纯文本
*/
public static String Html2Text(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
Pattern p_script;
Matcher m_script;
Pattern p_style;
Matcher m_style;
Pattern p_html;
Matcher m_html;
Pattern p_html1;
Matcher m_html1; 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标签的正则表达式
String regEx_html1 = "<[^>]+";
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标签 p_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
m_html1 = p_html1.matcher(htmlStr);
htmlStr = m_html1.replaceAll(""); // 过滤html标签 textStr = htmlStr; } catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
} return textStr;// 返回文本字符串
} /**
* 移除段落标签
*/
public static String removeP(String html) {
String result = html;
if (result.contains("<p>") && result.contains("</p>")) {
result = result.replace("<p>", "");
result = result.replace("</p>", "<br>");
while (result.endsWith("<br>")) {
result = result.substring(0, result.length() - 4);
}
}
return result;
}
}

1.2.这里面总共定义了两个静态方法。

  • 获取html中的纯文本
  • 移除段落标签

  除此之外,其他方法,可以看情况添加进去。

2.局部分析

2.1.如何获取html中的纯文本

  

  

  这里传进去一个html的字符串,然后通过一些过滤操作,返回一个纯文本。

  其实有多种方法都可以实现提取html纯文本==>Java实现从Html文本中提取纯文本

  关于正则表达式匹配Html标签==>正则表达式

  

  

  

2.2.移除段落标签

  

  先将<p>==>""

  将</p>==>"<br>"

  最后判断末尾是否有<br>标签,如果有则清除。

3.案例

3.1.测试转换html

  有如下html代码:

<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
body {color:red;}
h1 {color:#00ff00;}
p.ex {color:rgb(0,0,255);}
</style>
</head> <body>
<h1>这是标题 1</h1>
<p>这是一个普通的段落。请注意,本文是红色的。页面中定义默认的文本颜色选择器。</p>
<p class="ex">这是一个类为"ex"的段落。这个文本是蓝色的。</p>
</body>
</html>

  页面效果:

  

  然后转换过后的数据为:

  

3.2.测试移除段落标签

  测试几段文字

  <p>第一段</p>

  <p>第二段</p>

  <p>第三段</p>

  <p>第四段</p>

  <p>第五段</p>

  执行结果:

  

Android Html处理器通用类 HtmlUtil的更多相关文章

  1. Android 文件管理器通用类 FileUtil

    1.整体分析 1.1.源代码如下,可以直接Copy. public class FileUtil { private FileUtil() { } //****系统文件目录************** ...

  2. Android 异步请求通用类

    package com.example.demo1; import java.util.EventListener; public interface MyAsyncTaskListener exte ...

  3. Android Url相关工具 通用类UrlUtil

    1.整体分析 1.1.源代码查看,可以直接Copy. public class UrlUtil { public static boolean isUrlPrefix(String url) { re ...

  4. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  5. Android 通过 Intent 传递类对象或list对象

    (转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ...

  6. poi导出excel通用类

    一.关键的通用类public class PoiExportUtils {    private static HSSFWorkbook workBook; public PoiExportUtils ...

  7. NPOI MVC 模型导出Excel通用类

    通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...

  8. MVC NPOI Linq导出Excel通用类

    之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...

  9. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

随机推荐

  1. Python开发环境Wing IDE的Blender的Python代码调试技巧

    Wing IDE是一个集成开发环境,可用于开发.测试和调试为Blender编写的Python代码,Blender是一个开源的3 D内容创建系统.Wing IDE提供自动完成.调用提示.强大的调试器.以 ...

  2. JSP中的Property 'name' not found on type java.lang.String

    如果是在forEach中出现. 那么看下items里是不是没有el表达式,只是个字符串. 今天犯了好几次. 特此记录

  3. Struts2_用Action的属性接收参数

    先在 Action 中定义要接收的属性,需要编写属性的getter 和 setter 方法 struts2 会自动帮我们把 String 类型的参数转为 Action 中相对应的数据类型. priva ...

  4. Redis在Windows下安装全过程

    一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...

  5. 07、Spark集群的进程管理

    07.Spark集群的进程管理 7.1 概述 Spark standalone集群模式涉及master和worker两个守护进程.master进程是管理节点,worker进程是工作节点.spark提供 ...

  6. 洛谷 P3905 道路重建

    题目描述 从前,在一个王国中,在n个城市间有m条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有d条道路被破坏了.国王想要修复国家的道路系统,现在有两个重要城市A和B ...

  7. Torch.no_grad()影响MSE损失

    相关描述 https://discuss.pytorch.org/t/torch-no-grad-affecting-outputs-loss/28595/3 今天在训练网络的时候,发现mseloss ...

  8. spring boot1.5.6 测试类1

    package com.example.demo; import org.junit.Before;import org.junit.Test; import org.junit.runner.Run ...

  9. caffe中的sgd,与激活函数(activation function)

    caffe中activation function的形式,直接决定了其训练速度以及SGD的求解. 在caffe中,不同的activation function对应的sgd的方式是不同的,因此,在配置文 ...

  10. Centos防火墙添加IP白名单

    Centos iptables防火墙添加IP白名单,指定IP可访问端口 vi /etc/sysconfig/iptables 以下为我虚拟机的防火墙为例(Centos 7) # sample conf ...