使用XMLParser将节点插入GWT客户端中的现有XML文档

时间:2012-05-17 23:57:42

标签: java xml gwt

我有一个xml文档(使用GWT客户端的XMLParser库中的Document类),格式如下:

<document><node id="0">content</node><node id="1">more content</node></document>

给定一个ID,我需要在具有该ID的节点之后立即插入一个新节点。

到目前为止,我已尝试使用insertBefore(因为没有insertAfter),但我必须正确使用它,因为没有任何反应(除了js控制台中的UmbrellaException)。我无法通过搜索引擎找到任何示例用法。

我的尝试如下(其中n是我想要插入的节点):

Node nNext = n.getNextSibling(); //To get the next sibling to use it with insertBefore
Element newNode = doc.createElement("node");
newNode.appendChild(doc.createTextNode("new content")); //seems to work up until here
n.insertBefore(newNode, nNext); //so this line could be the problem?

1 个答案:

答案 0 :(得分:2)

必须在父节点上调用

insertBefore,因此:

n.getParentNode().insertBefore(newNode, n.getNextSibling());