How to delete child node of given node in XML file using DOM Parser in Java?

Problem Description:

Suppose we have following XML file as an example.

   <Employee id="1">
   </Employee >
  <Employee id="2">
   <Employee id="3">

Here requirement is to delete child node <age>34</age> for Employee whose id is 3.  Similarly user can delete any child node for any Employee.


1) Parse XML file or Create an instance of Document interface present in org.w3c.dom.* package.

For Creating an instance of Document interface we need to use DocumentBuilderFactoryDocumentBuilder classes present in javax.xml.parsers.* package and File class present in* package.

File file = new File(XML_file_location);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);

2) Enter node id, child name. Here node means Employee and child means property of Employee.

Delete child node of given node in XML file using DOM Parser
Figure 1

3) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.

NodeList list = document.getElementsByTagName("Employee");

4) Now one by one examine each child node Employee.

  • From all the child nodes that we got in Step 3 take one child node. It returns instance of Node interface present in org.w3c.dom.* package.
          Node node = list.item(i);
  • Check whether this node is Element node or not?
          if (node.getNodeType() == Node.ELEMENT_NODE) { }
  • If it is an Element node then typecast it and get its data (attributes and properties).
          Element element = (Element) node;
  • Get its id as follows.
          String id=element.getAttribute("id");
  • Compare this id with id of given Employee.
         if (String.valueOf(id).equals(element.getAttribute("id"))) {}
  • If id found then check whether given child node exists or not.
           Node current = element.getElementsByTagName(property).item(0);
  • If child node exists then get previous sibling of child node as follows.
          Node prev = current.getPreviousSibling();
  • If sibling is text node then delete it from given node Employee.
          if (prev != null && prev.getNodeType() == Node.TEXT_NODE && prev.getNodeValue().trim().length() == 0) {
  • Now delete selected child node from given node Employee.

5) Create an instance of Transformer class present in javax.xml.transform.* package. 

For creating an instance of Transformer class we need to use TransformerFactory class present in javax.xml.transform.* package.

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();

6) Set xml file indentation (Optional)

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

7) Create an instance of DOMSource class present in javax.xml.transform.dom.* package and pass instance of Document interface created in step 1.

DOMSource source = new DOMSource(document);

8) Create an instance of StreamResult class present in* package.

For creating an instance of StreamResult class first we need to create an instance of File class present in* package.

File file = new File(location_of_xml_file);
StreamResult result = new StreamResult(file);

9) Transform XML Source to a Result.

transformer.transform(source, result);

Software Required:

  1. JDK
  2. Eclipse
  3. Maven
Note : I suggest to use latest version of all the software. If you need any help regarding these software installation please comment below I will help you.

Project Structure:

Java XML DOM Parser Structure Part 7
Figure 2

Java Code:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employee id="1">
<Employee id="2">



Java XML DOM Parser Output Part 7
Figure 3

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employee id="1">
<Employee id="2">
Here you can see that child node <age>20</age> has been deleted for Employee whose id is 2.

Download Project

Click here for more questions on DOM Parser.


Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.

Let's Get Connected: Linkedin | Facebook |

How to delete child node of given node in XML file using DOM Parser in Java? How to delete child node of given node in XML file using DOM Parser in Java? Reviewed by Rohit Agarwal on 10/09/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.