html转换text-分段落,实现富文本导入word的格式转换,标签过滤

html转换text-分段落,实现富文本导入word的格式转换,标签过滤

一、工具类 html2Text

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.*;

public class Html2Text extends HTMLEditorKit.ParserCallback {

    private static Html2Text html2Text = new Html2Text();

    StringBuffer s;

    public Html2Text() {
    }

    public void parse(String str) throws IOException {
        InputStream iin = new ByteArrayInputStream(str.getBytes());
        Reader in = new InputStreamReader(iin);
        s = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        // the third parameter is TRUE to ignore charset directive
        delegator.parse(in, this, Boolean.TRUE);
        iin.close();
        in.close();
    }

    public void handleEndOfLineString(String eol) {
       
    }

    /**
      *按标签分割过滤后执行
      */
    public void handleText(char[] text, int pos) {
        s.append(text);
    }

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

    public static String getContent(String str) {
        try {
            html2Text.parse(str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return html2Text.getText();
    }
}

二、分段实现

/**
     * @Name        :getTextContentP
     * @Description :<富文本html转换text段落>
     * @Author      :gaogushenling
     * @Date        :2021/10/23 14:15
     * @Version     :1.0
     * @History     :<修改代码时说明>
     * @param       :xmlStr
     * @return      :List<String>
     */
    private List<String> getTextContentP(String xmlStr) {
        String s = xmlStr.replaceAll("div", "p");
        String[] ss = s.split("<p");
        List<String> textList = new ArrayList<>();
        for (String s1 : ss) {
	  String s2 = Html2Text.getContent("<p "+s1);
	  if (StringUtil.isNotEmpty(s2)){
            //textList.add(s2.replaceAll("\"(?<=\")(\\\\S+)(?=\")\"",""));
	    textList.add(s2);
	  }
        }
        if (textList.size() == 0) {
            textList.add("富文本文件是空的");
        }
        return textList;
    }

调用

    List<String> textList = getTextContentP("富文本(html格式)");

番外:如果标签不正规,以上方式无法正确分割

用下方正则替换 String s2 = Html2Text.getContent("<p "+s1); 即可 正则的意思是匹配 <> 内容

//html2txt
String s2 = ("<p " + s1).replaceAll("<[^>|^<]*>","");

还有一件事,如果你想问为什么用p来截断:分段落
下边是最后的结论

    private List<String> getTextContentP(String xmlStr) {
		List<String> textList = new ArrayList<>();
		//html2txt : 带标签,则进行格式清除
		Pattern p = Pattern.compile("<.*>");
		if (StringUtils.isNotEmpty(xmlStr)){
			if (p.matcher(xmlStr.trim()).find()){
				String s = xmlStr.replaceAll("div", "p");
				String[] ss = s.split("<p");
				for (String s1 : ss) {
					if (StringUtil.isNotEmpty(s1)){
						String s2 = ("<p " + s1).replaceAll("<[^>|^<]*>","");
						if (StringUtil.isNotEmpty(s2)) {
							textList.add(s2);
						}
					}
				}
			}
			if (textList.size() == 0) {
				textList.add(xmlStr);
			}
		}else {
			textList.add("富文本文件是空的");
		}
        return textList;
    }

学习正则 https://deerchao.cn/tutorials/regex/regex.htm

posted @ 2021-10-23 14:31  高谷深陵  阅读(537)  评论(0编辑  收藏  举报