首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java中将JTextPanes/JEditorPaneshtml内容清除为String?

如何在Java中将JTextPanes/JEditorPaneshtml内容清除为String?
EN

Stack Overflow用户
提问于 2018-09-18 01:34:28
回答 2查看 0关注 0票数 0

我尝试从JTextPane获得漂亮(清理)的文本内容。以下是JTextPane的示例代码:

代码语言:javascript
复制
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中看起来像这样:

一个考验

得到这种打印到控制台:

代码语言:javascript
复制
<html>
  <head>

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

使用了substring()和/或replace()代码,但使用起来很不舒服:

代码语言:javascript
复制
String text = textPane.getText ().replace ("<html> ... <body>\n    , "");

是否有任何简单的函数<b>从字符串中删除除-tags(内容)之外的所有其他标记?

有时JTextPane <p>会在内容周围添加-tags,所以我也想摆脱它们

代码语言:javascript
复制
<html>
  <head>

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

想获得带有标签的文字内容:

代码语言:javascript
复制
This <b>is</b> a <b>test</b>.
EN

回答 2

Stack Overflow用户

发布于 2018-09-18 10:23:10

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

票数 0
EN

Stack Overflow用户

发布于 2018-09-18 11:16:14

进行了子类化HTMLWriter和覆盖,startTagendTag跳过了所有标签<body>

代码语言:javascript
复制
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);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档