使用java对Html操作

前端开发   发布日期:2025年06月04日   浏览次数:161
  1. htmltxt
    import org.apache.commons.lang3.StringEscapeUtils;
  2. import org.jsoup.Jsoup;
  3. import org.jsoup.nodes.Document;
  4. import org.jsoup.nodes.Document.OutputSettings;
  5. import org.jsoup.safety.Whitelist;
  6. public class Html2Txt {
  7. public static String toPlainText(String html)
  8. {
  9. if (html == null)
  10. {
  11. return "";
  12. }
  13. final Document document =Jsoup.parse(html);
  14. final OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false);
  15. document.outputSettings(outputSettings);
  16. document.select("br").append("\\n");
  17. document.select("p").prepend("\\n");
  18. document.select("p").append("\\n");
  19. final String newHtml = document.html().replaceAll("\\\\n", "\n");
  20. final String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings);
  21. final String result = StringEscapeUtils.unescapeHtml4(plainText.trim());
  22. return result;
  23. }
  24. public static void main(String[] args){
  25. }
  26. }

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

java操作xml

  1. import org.dom4j.Attribute;
  2. import org.dom4j.Element;
  3. public class ElmUtils {
  4. public static Element addNode(Element elm, String nodeName, String txt) {
  5. Element node = elm.addElement(nodeName);
  6. txt = txt == null ? "" : txt;
  7. node.addText(txt);
  8. return node;
  9. }
  10. public static String getElmTxt(Element root, String elmNM) {
  11. Element tmpElm = (Element) root.selectSingleNode("//" + elmNM);
  12. if (tmpElm == null) {
  13. return "";
  14. } else {
  15. return tmpElm.getText();
  16. }
  17. }
  18. public static String elementTxt(Element root, String elmNM) {
  19. Element tmpElm = (Element) root.element(elmNM);
  20. if (tmpElm == null) {
  21. return "";
  22. } else {
  23. return tmpElm.getText();
  24. }
  25. }
  26. public static String getCurElmTxt(Element root, String elmNM) {
  27. Element tmpElm = (Element) root.selectSingleNode(elmNM);
  28. if (tmpElm == null) {
  29. return "";
  30. } else {
  31. return tmpElm.getText();
  32. }
  33. }
  34. public static String getElmAttrVal(Element root, String attrNM) {
  35. Attribute temAttr = root.attribute(attrNM);
  36. if (temAttr == null) {
  37. return "";
  38. } else {
  39. return temAttr.getValue();
  40. }
  41. }
  42. public static Element setElmTxt(Element elm, String elmName, String elmTxt) {
  43. Element tempElm = (Element) elm.selectSingleNode("//" + elmName);
  44. if(tempElm!=null){
  45. tempElm.setText(elmTxt);
  46. }
  47. return elm;
  48. }
  49. public static Element setElmAttr(Element elm, String attrName, String attrVal){
  50. Attribute attr = elm.attribute(attrName);
  51. attr.setValue(attrVal);
  52. return elm;
  53. }
  54. }

 

以上就是使用java对Html操作的详细内容,更多关于使用java对Html操作的资料请关注九品源码其它相关文章!