【问题标题】:JEditorPane and source of HTML elementJEdi​​torPane 和 HTML 元素的来源
【发布时间】:2011-12-20 02:50:03
【问题描述】:

Java 中的HTMLEditorKitHTMLDocument (仍然)有问题。我只能设置元素的内部 HTML,但我无法获取它。有什么办法,如何获取元素的基本 HTML 代码?

我的问题是,HTML 支持很差,而且写得不好。 API 不允许使用基本的和预期的功能。我需要更改<td>colspan 或rowspan attribute。 Java 开发人员关闭了直截了当的方法:元素的属性集是不可变的。解决方法可能是获取元素的代码(例如<td colspan="2">Hi <u>world</u></td>)并将其替换为新内容(例如<td colspan="3">Hi <u>world</u></td>)。这条路似乎也关闭了。 (额外问题:HTMLEditorKit 有什么用?)

【问题讨论】:

    标签: java html swing htmleditorkit


    【解决方案1】:

    你可以得到选中的Element html。使用套件的 write() 方法传递元素的偏移量。但它会包含在周围的标签“”“

    ”等中。

    【讨论】:

    • 啊...你是对的。该实现被优化为绝对防弹。将运动变成渗透器比自定义这个 Swing HTML 更容易。 (然而,官方文档说,可以轻松自定义特定元素的显示方式或为新类型元素添加功能)为什么实现缺少这些基本和可预期的功能,例如获取内部 HTML一些元素?
    【解决方案2】:

    感谢您的提示,斯坦尼斯拉夫。这就是我的解决方案:

    /**
     * The method gets inner HTML of given element. If the element is named <code>p-implied</code>
     * or <code>content</code>, it returns null.
     * @param e element
     * @param d document containing given element
     * @return the inner HTML of a HTML tag or null, if e is not a valid HTML tag
     * @throws IOException
     * @throws BadLocationException
     */
    public String getInnerHtmlOfTag(Element e, Document d) throws IOException, BadLocationException {
        if (e.getName().equals("p-implied") || e.getName().equals("content"))
            return null;
    
        CharArrayWriter caw = new CharArrayWriter();
        int i;
        final String startTag = "<" + e.getName();
        final String endTag = "</" + e.getName() + ">";
        final int startTagLength = startTag.length();
        final int endTagLength = endTag.length();
    
        write(caw, d, e.getStartOffset(), e.getEndOffset() - e.getStartOffset());
        //we have the element but wrapped as full standalone HTML code beginning with HTML start tag
        //thus we need unpack our element
        StringBuffer str = new StringBuffer(caw.toString());
        while (str.length() >= startTagLength) {
            if (str.charAt(0) != '<')
                str.deleteCharAt(0);
            else if (!str.substring(0, startTagLength).equals(startTag))
                str.delete(0, startTagLength);
            else
                break;
        }
        //we've found the beginning of the tag
        for (i = 0; i < str.length(); i++) { //skip it...
            if (str.charAt(i) == '>')
                break; //we've found end position of our start tag
        }
        str.delete(0, i + 1); //...and eat it
        //skip the content
        for (i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '<' && i + endTagLength < str.length() && str.substring(i, i + endTagLength).equals(endTag))
                break; //we've found the end position of inner HTML of our tag
        }
        str.delete(i, str.length()); //now just remove all from i position to the end
    
        return str.toString().trim();
    }
    

    可以轻松修改此方法以获取外部 HTML(因此代码包含整个标签)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-15
      • 2011-06-06
      • 1970-01-01
      • 2011-08-20
      • 2012-07-29
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多