0

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>
  • Note: I need to maintain order of nodes.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

How can i insert nodes at specific location?
Found my solution at, stackoverflow.com/questions/3247577/… Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.