سؤال

I have a xml document with simple org.w3c.dom.node nodes inside it . Adding a new node always happens at the end . However I want that if I have identified a node with value say 3 , I should always insert a new node just after this node .

Whenever when I try to add a node via

nodeWithValue3.getParentNode().appendChild(newNode);

it always happens at the end of the document .

هل كانت مفيدة؟

المحلول 2

Since i took some time to figure the answer , answering here so someone might get help from this

I found that I can do something like below to achieve the same .

node nextNode = nodeWithValue3.getNextSibling();
nodeWithValue3.getParentNode().insertBefore(newChildNode, nextNode);

نصائح أخرى

I think you must read the whole xml file node by node,then write those nodes to a new xml file.During the write operation,you can write a new node once write the specified node,such as nodeWithValue3.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top