JEdi​​torPane 和 HTML 元素的来源

发布于 2024-12-07 22:48:08 字数 428 浏览 1 评论 0原文

我在 Java 中的 HTMLEditorKitHTMLDocument 方面仍然存在问题。我只能设置元素的内部 HTML,但无法获取它。有什么方法可以获取元素的底层 HTML 代码吗?

我的问题是,HTML 支持很差并且写得不好。该 API 不允许基本的和预期的功能。我需要更改 colspan 或 rowspan attribute。 Java开发者关闭了最直接的方法:元素的属性集是不可变的。解决方法可能是获取元素的代码(例如 Hi world)并将其替换为新的内容(例如 Hi world)。这条路似乎也被封闭了。 (额外问题:HTMLEditorKit 有什么用处?)

I have (still) problems with HTMLEditorKit and HTMLDocument in Java. I can only set the inner HTML of an element, but I cannot get it. Is there some way, how to get a uderlying HTML code of an element?

My problem is, that the HTML support is quite poor and bad written. The API does not allow basic and expected functions. I need change the colspan or rowspan attribute of <td>. The Java developers have closed the straightforward way: the attribute set of element is immutable. The workaround could be to take the code of element (e.g. <td colspan="2">Hi <u>world</u></td>) and replace it with new content (e.g. <td colspan="3">Hi <u>world</u></td>). This way seems to be closed too. (Bonus question: What's the HTMLEditorKit good for?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2024-12-14 22:48:08

您可以获得所选元素的html。使用套件的 write() 方法传递元素的偏移量。但它将包含在周围的标签“”中“<身体>” ETC。

You can get the selected Element html. Use write() method of the kit passing there offsets of the Element. But it will be included with surrounding tags "<html>" "<body>" etc.

迷鸟归林 2024-12-14 22:48:08

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

/**
 * 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(因此代码包含整个标记)。

Thanks for hint, Stanislav. That's my solution:

/**
 * 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();
}

This method can be easilly modified to get outter HTML (so the code containing the entire tag).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文