문제

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