Modify XML file in Java using DOM parser
In one of the earlier post we saw How to parse an XML using DOM parser.
In this post we will see how to modify XML file in Java using DOM parser. We can add, update and delete any nodes/elements in XML using DOM parsers in Java.
Lets see the example. We have the below XML that we will modify
Original XML
1 2 3 4 5 6 7 8 9 10 11 12 | <employeeDetails> <employee id="0001"> <name>John Doe</name> <department>Sales</department> <salary>$5000</salary> </employee> <employee id="0002"> <name>Scott Tiger</name> <department>HR</department> <salary>$4000</salary> </employee> </employeeDetails> |
Modify XML file in Java using DOM parser
Now we will perform the following actions on the XML
1. Update the Salary Of the First Employee to 6500
2. Add element “Age” to the First Employee
3. Remove the department from Second Employee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.kscodes.sampleproject; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ModifyXmlDomParser { public static void main(String[] args) { try { String xmlFilePath = "C:\\kscodes\\employee.xml"; DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File(xmlFilePath)); // Update the Salary Of the First Employee Node firstEmployee = document.getElementsByTagName("employee") .item(0); NodeList firstEmpNodeList = firstEmployee.getChildNodes(); for (int i = 0; i < firstEmpNodeList.getLength(); i++) { Node element = firstEmpNodeList.item(i); if ("salary".equals(element.getNodeName())) { element.setTextContent("$6500"); } } // Add element "Age" to the First Employee Element age = document.createElement("age"); age.appendChild(document.createTextNode("30")); firstEmployee.appendChild(age); // Remove the department from Second Employee Node secondEmployee = document.getElementsByTagName("employee") .item(1); NodeList secondEmpNodeList = secondEmployee.getChildNodes(); for (int i = 0; i < secondEmpNodeList.getLength(); i++) { Node element = secondEmpNodeList.item(i); if ("department".equals(element.getNodeName())) { secondEmployee.removeChild(element); } } // write the content into xml file TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(xmlFilePath)); transformer.transform(source, result); System.out.println("Changes to the XML completed !!!!"); } catch (Exception e) { System.out.println("Exception occured" + e); e.printStackTrace(); } } } |
Modified XML
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> <employeeDetails> <employee id="0001"> <name>John Doe</name> <department>Sales</department> <salary>$6500</salary> <age>30</age> </employee> <employee id="0002"> <name>Scott Tiger</name> <salary>$4000</salary> </employee> </employeeDetails> |