java - 如何将 JTextPanes/JEditorPanes html 内容清理为 Java 中的字符串?

标签 java html string jtextpane

我尝试从 JTextPane 获取漂亮(干净)的文本内容。这是来自 JTextPane 的示例代码:

JTextPane textPane = new JTextPane ();
textPane.setContentType ("text/html");
textPane.setText ("This <b>is</b> a <b>test</b>.");
String text = textPane.getText ();
System.out.println (text);

JTexPane 中的文本看起来像这样:

This is a test.

我将这种打印输出到控制台:

<html>
  <head>

  </head>
  <body>
    This <b>is</b> a <b>test</b>.
  </body>
</html>

我用过 substring()和/或 replace()代码,但是用起来不舒服:

String text = textPane.getText ().replace ("<html> ... <body>\n    , "");

除了<b> 之外,是否有任何简单的函数可以删除所有其他标签?来自字符串的标签(内容)?

有时 JTextPane添加 <p>内容周围的标签,所以我也想摆脱它们。

像这样:

<html>
  <head>

  </head>
  <body>
    <p style="margin-top: 0">
      hdfhdfgh
    </p>
  </body>
</html>

我只想获取带有标签的文本内容:

This <b>is</b> a <b>test</b>.

最佳答案

我子类化了 HTMLWriter 并覆盖 startTagendTag跳过 <body> 之外的所有标签.

我没有测试太多,它似乎工作正常。一个缺点是输出字符串有很多空格。摆脱它不应该太难。

import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class Foo {

    public static void main(String[] args) throws Exception {
        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        textPane.setText("<p>This</p> <b>is</b> a <b>test</b>.");

        StringWriter writer = new StringWriter();
        HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();

        HTMLWriter htmlWriter = new OnlyBodyHTMLWriter(writer, doc);
        htmlWriter.write();

        System.out.println(writer.toString());
    }

    private static class OnlyBodyHTMLWriter extends HTMLWriter {

        public OnlyBodyHTMLWriter(Writer w, HTMLDocument doc) {
            super(w, doc);
        }

        private boolean inBody = false;

        private boolean isBody(Element elem) {
            // copied from HTMLWriter.startTag()
            AttributeSet attr = elem.getAttributes();
            Object nameAttribute = attr
                    .getAttribute(StyleConstants.NameAttribute);
            HTML.Tag name = null;
            if (nameAttribute instanceof HTML.Tag) {
                name = (HTML.Tag) nameAttribute;
            }
            return name == HTML.Tag.BODY;
        }

        @Override
        protected void startTag(Element elem) throws IOException,
                BadLocationException {
            if (inBody) {
                super.startTag(elem);
            }
            if (isBody(elem)) {
                inBody = true;
            }
        }

        @Override
        protected void endTag(Element elem) throws IOException {
            if (isBody(elem)) {
                inBody = false;
            }
            if (inBody) {
                super.endTag(elem);
            }
        }
    }
}

关于java - 如何将 JTextPanes/JEditorPanes html 内容清理为 Java 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5499758/

相关文章:

html - 坚持在移动设备上对齐,在桌面上完美运行

javascript - 字符串替换为 'any character' 掩码问题

java - 触摸按钮时发出哔声

java - 为简单的 Java Web 应用程序使用框架是否有意义?

html - 登录模式的 ASP.NET Web 窗体问题

string - 近似字符串匹配的具体算法代码

javascript - jQuery 字符串包含操作?

java - 实体持久性注册验证监听器

java - 在 Jenkins 中以自定义格式显示 TestNG 错误消息

javascript - 使用一个 jquery 代码关闭多个模式