John
John

Reputation: 222

Inserting nodes into an existing XML document in GWT client using XMLParser

I have an xml document (using the Document class in the XMLParser library of GWT client) with a format like follows:

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

Given an ID, I need to insert a new node immediately after the node with that ID.

So far I've tried using insertBefore (as there is no insertAfter), but I must be using it incorrectly as nothing happens (apart from an UmbrellaException in the js console). I can't find any example usage via search engines.

My attempt is as follows (where n is the node I want to insert after):

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?

Upvotes: 1

Views: 315

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64541

insertBefore must be called on the parent node, so:

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

Upvotes: 2

Related Questions