Replacing Nodes in a Tree
There are times when you want to replace a node rather than add a new one. To understand why this is important, recall that the XML code is only one part of the total package. In general, there will be application code that will write the data currently in XML form to a database. If the data is to be read and updated by removing an entry and then adding one, it makes more sense to perform a replacement rather than the two separate operations of deletion and insertion.
The replacement can be done by using the replaceChild(newChild,oldChild) method. As the names imply, the node pointed to by oldChild is replaced by the node pointed to by newChild.
In the following example, you'll first clone the documentElement node of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access