2

我尝试从 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

一个测试

我得到这种打印到控制台:

<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>.
4

4 回答 4

5

我继承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);
            }
        }
    }
}
于 2011-03-31T21:29:28.723 回答
1

您可以使用 JEditorPane 自己使用的 HTML 解析器,HTMLEditorKit.ParserDelegator.

请参阅此示例API 文档。

于 2011-03-31T20:59:52.840 回答
0

我通过使用 substring 和 replace -methods 找到了解决此问题的方法:

// Get textPane content to string
String text = textPane.getText();

// Then I take substring to remove tags (html, head, body)
text = text.substring(44, text.length() - 19);

// Sometimes program sets <p style="margin-top: 0"> and </p> -tags so I remove them
// This isn't necessary to use.
text = text.replace("<p style=\"margin-top: 0\">\n      ", "").replace("\n    </p>", ""));

// This is for convert possible escape characters example &amp; -> &
text = StringEscapeUtils.unescapeHtml(text);

有指向 StringEscapeUtils -libraries 的链接,可将转义字符转换回正常视图。感谢 Ozhan Duz 的建议。

( commons- lang-下载)

于 2011-03-31T19:59:51.663 回答
0
String text = textPane.getDocument.getText (0,textPane.getText().length());
于 2018-09-11T19:31:40.007 回答