在 dom 树中的第一个子节点之前添加一个新节点
这可能是最容易处理的事情之一,但由于某种原因对我不起作用。我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要创建新的
Document
或新的根Element
?我认为最短的方法是使用Branch#content
:您只需创建新元素并通过
content
方法提供的List
将其添加到根元素(向其传递位置索引),这是我的主要内容:它打印出以下 xml:
另外,当您必须转换 xml 时,还应该考虑使用 xslt。
Why create a new
Document
or a new rootElement
? I think the shortest way is usingBranch#content
:You just have to create the new Element and to add it to the root element through the
List
provided bycontent
method (passing it the position index), this is my main:which prints out the following xml:
In addition, you should also consider the use of xslt when you have to transform xml.
您正在调用
Element#add(Entity)
。来自 Java 文档:因此,您要添加的新节点将作为您要添加到的节点的子添加。您不能在根节点之后添加另一个节点,因为文档只能有一个根节点。
您可以做的是创建一个新的根节点,然后将旧根节点和新节点添加为该新根节点的子节点。然后将文档的根节点设置为新的根节点。
You're calling
Element#add(Entity)
. From the Javadocs: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.
为什么不直接创建一个具有所需值的新文档,然后将其他节点附加到其中?
Why not just create a new Document with the value you want, then append the other nodes to it?