728x90 AdSpace

Saturday, 9 November 2013

Modify XML file using DOM Parser

In this tutorials we going to see how to modify (add new element, update attribute, update element, delete element )the existing XML document using DOM parser
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.dom under JavaXML.
3. Create Java class called ModifyXML.java under com.javatutorialscorner.xml.dom package.
package com.javatutorialscorner.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
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"));

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

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
if ("department".equals(node.getNodeName())) {

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(
transformer.transform(domSource, streamResult);

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




Original XML file

<?xml version="1.0" encoding="UTF-8"?>
<student id="1">
<student id="2">

Now run the program see the XML file modified.

Modified XML file

<?xml version="1.0" encoding="UTF-8" standalone="no"?><college>
<student id="ECE-01">
<year>final year</year>
<student id="2">
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Modify XML file using DOM Parser Rating: 5 Reviewed By: Annamalai Thangaraj