Apr 02
|
To insert a new root node into an existing DOM Document involves creating a new DOM Document with the required new root node, and then copying in the existing DOM Document into the new root.
The following code snippets shows an outline of the code involved.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document existingdoc = builder.parse(file); // Create an empty document Document doc = builder.newDocument(); // Add the new root node Element root = doc.createElement("Objects"); doc.appendChild(root); // Add a copy of the nodes from existing document Node copy = doc.importNode(existingdoc.getDocumentElement(), true); root.appendChild(copy);