Tejas jain
Tejas jain

Reputation: 752

Replace XML Node with Array of Nodes using java

How can I replace a Node with Array of Nodes?

I tried:

specialNode.getParentNode().replaceChild(newNode[i], specialNode);

For example:
Input: PathToXMLFile and SpecialKeys[] = {"value-2","value-3","value-4"}

<root>
    <childOne>
        <otherNode />
        <otherNode />
        <specialNode key="value-1">
        </specialNode>
        <otherNode />
    </childOne>
    <childTwo>
    </childTwo>
</root>

Required output:

<root>
    <childOne>
        <otherNode />
        <otherNode />
        <specialNode key="value-2">
        </specialNode>
        <specialNode key="value-3">
        </specialNode>
        <specialNode key="value-4">
        </specialNode>
        <otherNode />
     </childOne>
     <childTwo>
     </childTwo>
</root>

Upvotes: 0

Views: 79

Answers (1)

Sridhar
Sridhar

Reputation: 1962

In this case you have to do either

  • insert new nodes at and delete old node (or)

  • replace the parent itself, ie. construct ChildOne with new content of children nodes and replace

Upvotes: 1

Related Questions