java - 使用 Jsoup 删除所有 HTML 但保留行

标签 java html regex jsoup

我有一个 String,其中包含一封电子邮件的一些内容,我想从这个 String 中删除所有 HTML 编码。

这是我目前的代码:

public static String html2text(String html) {

   Document document = Jsoup.parse(html);
   document = new Cleaner(Whitelist.basic()).clean(document);
   document.outputSettings().escapeMode(EscapeMode.xhtml);
   document.outputSettings().charset("UTF-8");
   html = document.body().html();

   html = html.replaceAll("<br />", "");

   splittedStr = html.split("Geachte heer/mevrouw,");

   html = splittedStr[1];

   html = "Geachte heer/mevrouw,"+html;

   return html;
}

此方法删除所有 HTML,保留行和大部分布局。但它也会返回一些 &nbsp; 标签,这些标签没有被完全删除。看下面的输出,你可以看到 String 中仍然有一些标签,甚至是标签的一部分。我该如何摆脱这些?

  Loonheffingen       &amp;n= bsp; Naam
 nr         in administratie         &amp;nbs= p;           meldingen
  nummer

 1          &amp;n= bsp;            = ;     0            &amp;= nbsp;           &amp;nbs= p;           1
           123456789L01

编辑:

<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">De afgekeurde meldingen zijn opgenomen in de bijlage: Afgekeurde meldingen.</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">

<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Wilt u zo spoedig mogelijk zorgdragen dat deze</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">meldingen gecorrigeerd worden aangeleverd?</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">mer</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Volg &nbsp; &nbsp; Aantal verwerkt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Aantal afgekeurde</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">&nbsp;Loonheffingen &nbsp; &nbsp; &nbsp; &nbsp; Naam</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">nr &nbsp; &nbsp; &nbsp; &nbsp; in administratie &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meldingen</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">&nbsp;nummer</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">

这是我要解析的 HTML 的一部分。我想删除所有 HTML,但保留原始电子邮件的布局。

感谢任何帮助,

谢谢!

已解决

        Document xmlDoc = Jsoup.parse(file, "", Parser.xmlParser());
        Elements spans= xmlDoc.select("span");

        for (Element link : spans) {
            String html = textPlus(link);
            System.out.println(html);
        }


 public static String textPlus(Element elem) {
    List<TextNode> textNodes = elem.textNodes();
    if (textNodes.isEmpty()) {
        return "";
    }

    StringBuilder result = new StringBuilder();
    // start at the first text node
    Node currentNode = textNodes.get(0);
    while (currentNode != null) {
        // append deep text of all subsequent nodes
        if (currentNode instanceof TextNode) {
            TextNode currentText = (TextNode) currentNode;
            result.append(currentText.text());
        } else if (currentNode instanceof Element) {
            Element currentElement = (Element) currentNode;
            result.append(currentElement.text());
        }
        currentNode = currentNode.nextSibling();
    }
    return result.toString();
}

代码是作为 this 问题的答案提供的。

最佳答案

您需要遍历 JSoup 返回的 HTML 结构并整理文本节点,而不是这样做。这样您就可以让 JSoup 确定什么是真正的文本,实体编码将为您处理(例如 & -> & 等)。

参见 this SO question了解更多信息。

关于java - 使用 Jsoup 删除所有 HTML 但保留行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988452/

相关文章:

java - 使用 Java Fitnesse 进行 XML 输入/输出

java - Spring Boot + Java : Keyword Based Search from JSON data

javascript - 如何隐藏 div 并使其可点击以显示媒体屏幕的叠加层

javascript - 如何制作显示事件时间的刻度?

r - 仅在顶层拆分带有嵌套括号的字符串,其中 "level"由括号确定

java - char[] 上的流写入器不起作用

java - 如何在原因未知时调试问题

html - 使用 Nokogiri 解析 HTML

java - 匹配键值模式正则表达式

java - 从给定字符串替换\n\r 和\t 的最佳方法