• 使用java对Html操作


    html转txt
    import
    org.apache.commons.lang3.StringEscapeUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Document.OutputSettings; import org.jsoup.safety.Whitelist; public class Html2Txt { public static String toPlainText(String html) { if (html == null) { return ""; } final Document document =Jsoup.parse(html); final OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false); document.outputSettings(outputSettings); document.select("br").append("\n"); document.select("p").prepend("\n"); document.select("p").append("\n"); final String newHtml = document.html().replaceAll("\\n", " "); final String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings); final String result = StringEscapeUtils.unescapeHtml4(plainText.trim()); return result; } public static void main(String[] args){ } }

    -----------------

    java操作xml

    import org.dom4j.Attribute;
    import org.dom4j.Element;
    
    
    public class ElmUtils {
        public static Element addNode(Element elm, String nodeName, String txt) {
            Element node = elm.addElement(nodeName);
            txt = txt == null ? "" : txt;
            node.addText(txt);
            return node;
        }
    
        public static String getElmTxt(Element root, String elmNM) {
            Element tmpElm = (Element) root.selectSingleNode("//" + elmNM); 
            if (tmpElm == null) {
                return "";
            } else {
                return tmpElm.getText();
            }
        }
        
        public static String elementTxt(Element root, String elmNM) {
            Element tmpElm = (Element) root.element(elmNM); 
            if (tmpElm == null) {
                return "";
            } else {
                return tmpElm.getText();
            }
        }
        
        public static String getCurElmTxt(Element root, String elmNM) {
            Element tmpElm = (Element) root.selectSingleNode(elmNM);
            if (tmpElm == null) {
                return "";
            } else {
                return tmpElm.getText();
            }
        }
        
        public static String getElmAttrVal(Element root, String attrNM) {
            Attribute temAttr = root.attribute(attrNM);
            if (temAttr == null) {
                return "";
            } else {
                return temAttr.getValue();
            }
        }
        
    
        public static Element setElmTxt(Element elm, String elmName, String elmTxt) {
            Element tempElm = (Element) elm.selectSingleNode("//" + elmName);
            if(tempElm!=null){
                tempElm.setText(elmTxt);
            }
            return elm;
        }
        
        public static Element setElmAttr(Element elm, String attrName, String attrVal){
            Attribute attr = elm.attribute(attrName);
            attr.setValue(attrVal);
            return elm;
        }
    }
  • 相关阅读:
    Selenium协议
    seleniumxpath定位方法详解
    selenium 数据驱动测试 ddt
    为什么要使用Gard代替if判断
    TCP的爱情故事
    C#面试之 值类型和引用类型区别
    Qt QFileSystemModel 的使用
    QStringListModel的使用
    工作问题记录:elementUI 中表单校验问题
    c++读书笔记
  • 原文地址:https://www.cnblogs.com/liangblog/p/13949682.html
Copyright © 2020-2023  润新知