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 ...

Get Sams Teach Yourself XML in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.