使用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", "\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;
    }
}

 

posted @ 2020-11-09 17:31  凉城  阅读(1539)  评论(0编辑  收藏  举报