The document element cannot be changed. Which is why there is no setDocumentElement() actually. So solution 2 is the only option. The problem you're facing with it is easy to solve. You've actually fallen into one of the most common traps of the DOM. NodeLists are live! So, with the loop:
> NodeList list = oldNode.getChildNodes(); > for (int i = 0; i < list.getLength(); i++) > oldNode.removeChild(list.item(i)); You're missing several nodes because the list you're iterating over is being changed every time a node is removed. To avoid that you should do instead: NodeList list = oldNode.getChildNodes(); while (list.getLength() > 0) oldNode.removeChild(list.item(0)); or: while (oldNode.hasChildNodes()) oldNode.removeChild(oldNode.getFirstChild()); The latter being the most efficient way of doing this. -- Arnaud Le Hors - IBM Cupertino, XML Technology Group
