How to Modify XML file using DOM Parser

In this article we are going to see about how to modify
existing XML file using DOM parser. This example covers how to
add new element, update attribute, update element, delete element in already existing xml file. Read our previous articles for how to read xml file using DOM parser.

ModifyXML.java

package com.ehowtonow.xml.dom;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ModifyXML {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  try {
   File xmlFile = new File("C:\\jtc\\student.xml");
   DocumentBuilderFactory builderFactory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder builder = builderFactory.newDocumentBuilder();
   Document document = builder.parse(xmlFile);

   // get first element node of root node
   Node studentNode = document.getElementsByTagName("student").item(0);

   // add new Element
   Element year = document.createElement("year");
   year.appendChild(document.createTextNode("final year"));
   studentNode.appendChild(year);

   // update attribute
   NamedNodeMap arrtMap = studentNode.getAttributes();
   Node attrNode = arrtMap.getNamedItem("id");
   attrNode.setTextContent("ECE-01");

   NodeList nodeList = studentNode.getChildNodes();

   for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);

    if ("lastname".equals(node.getNodeName())) {
     // update text node
     node.setTextContent("k");
    }
    if ("department".equals(node.getNodeName())) {
     studentNode.removeChild(node);
    }
   }

   TransformerFactory factory = TransformerFactory.newInstance();
   Transformer transformer = factory.newTransformer();
   DOMSource domSource = new DOMSource(document);
   StreamResult streamResult = new StreamResult(new File(
     "C:\\jtc\\student.xml"));
   transformer.transform(domSource, streamResult);

  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (TransformerConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (TransformerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

student.xml
Original XML file

<?xml version="1.0" encoding="UTF-8"?>
<college>
<student id="1">
<firstname>Shiva</firstname>
<lastname>Kumar</lastname>
<department>ECE</department>
</student>
<student id="2">
<firstname>Appu</firstname>
<lastname>T</lastname>
<department>CSE</department>
</student>
</college>

Modified XML file

<?xml version="1.0" encoding="UTF-8" standalone="no"?><college>
<student id="ECE-01">
<firstname>Shiva</firstname>
<lastname>k</lastname>
<year>final year</year>
</student>
<student id="2">
<firstname>Appu</firstname>
<lastname>T</lastname>
<department>CSE</department>
</student>
</college>

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. How to Read XML File using DOM parser
  2. How to Count number of Element in XML
  3. How to read simple XML file using DOM Parser
  4. How to Modify XML file using DOM Parser
  5. How to create XML file using DOM Parser
  6. Java DOM XML Parser

Be the first to comment

Leave a Reply