在 dom 树中的第一个子节点之前添加一个新节点

发布于 2024-10-31 09:46:20 字数 1029 浏览 0 评论 0原文

这可能是最容易处理的事情之一,但由于某种原因对我不起作用。我正在尝试在 dom 树的根之后添加一个新节点。
这是原始字符串:

<div class="discussionThread dt"><div class="dt_subject">2011 IS HERE!</div></div>

我正在尝试添加一个新节点,其形式为 before 。最终版本应如下所示:

<div class="discussionThread dt"><div class="test">Test Val</div><div class="dt_subject">2011 IS HERE!</div></div>

如您所见,新的 Test Val 立即添加到根 div 类之后。我使用了几种方法将节点放置在正确的位置,但它被附加在最后。

这是我从之前的一篇文章中引用的示例:

String newNode = "<div class="test">test</div>";
SAXReader reader = new SAXReader();
Document newNodeDocument = reader.read(new StringReader(newNode));
Document originalDoc = new SAXReader().read(new StringReader(content));
Element root = originalDoc.getRootElement();
Element givenNode = originalDoc.getRootElement(); 
givenNode.add(newNodeDocument.getRootElement());

这导致节点在末尾添加。我尝试使用 insertBefore(),但没有成功。

任何指示都将受到高度赞赏。

谢谢

This is probably one of those easiest things to deal with but for some reason not working for me. I'm trying to add a new node after the root in dom tree.
Here's the original string:

<div class="discussionThread dt"><div class="dt_subject">2011 IS HERE!</div></div>

I'm trying to add a new node which is in the form of a string before . The final version should look like:

<div class="discussionThread dt"><div class="test">Test Val</div><div class="dt_subject">2011 IS HERE!</div></div>

As you can see, the new Test Val is being added immediately after the root div class. I've used few methods to place the node at the right place but its getting appended at the end.

Here's a sample which I referred from one of the earlier posts:

String newNode = "<div class="test">test</div>";
SAXReader reader = new SAXReader();
Document newNodeDocument = reader.read(new StringReader(newNode));
Document originalDoc = new SAXReader().read(new StringReader(content));
Element root = originalDoc.getRootElement();
Element givenNode = originalDoc.getRootElement(); 
givenNode.add(newNodeDocument.getRootElement());

This is resulting the node getting added at the end. I tried using insertBefore(), but didn't work out.

Any pointers will be highly appreciated.

Thanks

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

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

发布评论

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

评论(3

绮烟 2024-11-07 09:46:21

为什么要创建新的 Document 或新的根 Element?我认为最短的方法是使用 Branch#content

返回该内容的节点
分支作为支持列表,以便
该分支的内容可能是
直接使用接口修改。
该名单得到分行的支持,因此
反映对列表的更改
在分支机构中,反之亦然。

您只需创建新元素并通过 content 方法提供的 List 将其添加到根元素(向其传递位置索引),这是我的主要内容:

public static void main(String[] args) throws DocumentException {

    SAXReader reader = new SAXReader();
    String xml = "<div class=\"discussionThread dt\"><div class=\"dt_subject\">2011 IS HERE!</div></div>";
    Document document = reader.read(new StringReader(xml));

    DefaultElement newElement = new DefaultElement("div");
    newElement.addAttribute("class", "test");
    newElement.add(new DefaultText("Test Val"));

    List content = document.getRootElement().content();
    if (content != null ) {
        content.add(0, newElement);
    }

    System.out.println(document.asXML());
}

它打印出以下 xml:

<div class="discussionThread dt"><div class="test">Test Val</div><div class="dt_subject">2011 IS HERE!</div></div>

另外,当您必须转换 xml 时,还应该考虑使用 xslt。

Why create a new Document or a new root Element? I think the shortest way is using Branch#content:

Returns the content nodes of this
branch as a backed List so that
the content of this branch may be
modified directly using the interface.
The List is backed by the Branch so
that changes to the list are reflected
in the branch and vice versa.

You just have to create the new Element and to add it to the root element through the List provided by content method (passing it the position index), this is my main:

public static void main(String[] args) throws DocumentException {

    SAXReader reader = new SAXReader();
    String xml = "<div class=\"discussionThread dt\"><div class=\"dt_subject\">2011 IS HERE!</div></div>";
    Document document = reader.read(new StringReader(xml));

    DefaultElement newElement = new DefaultElement("div");
    newElement.addAttribute("class", "test");
    newElement.add(new DefaultText("Test Val"));

    List content = document.getRootElement().content();
    if (content != null ) {
        content.add(0, newElement);
    }

    System.out.println(document.asXML());
}

which prints out the following xml:

<div class="discussionThread dt"><div class="test">Test Val</div><div class="dt_subject">2011 IS HERE!</div></div>

In addition, you should also consider the use of xslt when you have to transform xml.

橘虞初梦 2024-11-07 09:46:21

您正在调用 Element#add(Entity)。来自 Java 文档:

将给定的Entity 添加到此元素。如果给定节点已经定义了父节点,则将抛出 IllegalAddException

因此,您要添加的新节点将作为您要添加到的节点的添加。您不能在根节点之后添加另一个节点,因为文档只能有一个根节点。

您可以做的是创建一个新的根节点,然后将旧根节点和新节点添加为该新根节点的子节点。然后将文档的根节点设置为新的根节点。

You're calling Element#add(Entity). From the Javadocs:

Adds the given Entity to this element. If the given node already has a parent defined then an IllegalAddException will be thrown.

So the new node you're adding will be added as a child of the node you're adding it to. You cannot add another node after the root node you have, because a document can have only one root node.

What you can do is create a new root node, then add the old root node and the new node as children of this new root node. Then set the root node of the document to the new root node.

茶底世界 2024-11-07 09:46:21

为什么不直接创建一个具有所需值的新文档,然后将其他节点附加到其中?

Why not just create a new Document with the value you want, then append the other nodes to it?

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