Java DOM edit: Replacing an Existing Node with a New One
public void replacePerson(Document doc, String name,String phone,String email) {
Element newPersonNode = doc.createElement("person");
Element nameNode = doc.createElement("name");
newPersonNode.appendChild(nameNode);
Text nametextNode = doc.createTextNode(name);
nameNode.appendChild(nametextNode);
Element phoneNode = doc.createElement("phone");
newPersonNode.appendChild(phoneNode);
Text phonetextNode = doc.createTextNode(phone);
phoneNode.appendChild(phonetextNode);
Element emailNode = doc.createElement("email");
newPersonNode.appendChild(emailNode);
Text emailtextNode = doc.createTextNode(email);
emailNode.appendChild(emailtextNode);
Element root = doc.getDocumentElement();
Element oldPersonNode = (Element)root.getFirstChild();
root.replaceChild(newPersonNode,oldPersonNode);
}
Related examples in the same category