728x90 AdSpace

Saturday, 9 November 2013

Read XML File using DOM parser

In this tutorial we are going to see how to read XML file using DOM Parser. In this example I used to read XML document by Element Name.
DOM parser parse the entire XML document and load it into memory.DOM parse the nodes as tree structure which is easy for traversal or manipulation.
DOM parser slow compare to other parsers and it consumes lot of memory.You can use SAX parser instead of DOM.SAX parser if faster than DOM and uses less memory.
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.dom under JavaXML.
3. Create Java class called ReadXMLFile.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 org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXMLFile {

* @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);
System.out.println("Root Node :"
+ document.getDocumentElement().getNodeName());
NodeList list = document.getElementsByTagName("student");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
System.out.println("Child Element : " + node.getNodeName());

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Student ID : "
+ element.getAttribute("id"));
System.out.println("First Name : "
+ element.getElementsByTagName("firstname").item(0)
System.out.println("Last Name : "
+ element.getElementsByTagName("lastname").item(0)
System.out.println("Department : "
+ element.getElementsByTagName("department")

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



In above program document.getDocumentElement().normalize(); is option element but its recommended.Consider the following XML tag



 which is denormalized as follows

Element firstname

Text node : “”

Text node : “Shiva”

Text node : “Kumar”

Text node : “”

When its is normalized

Element firstname

Text node : “Shiva Kumar”

XML File


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

Now you can run the program see the following output in console.

Root Node :college


Child Element : student

Student ID : 1

First Name : Shiva

Last Name : Kumar

Department : ECE



Child Element : student

Student ID : 2

First Name : Appu

Last Name : T

Department : CSE

Annamalai Thangaraj

Annamalai is a Sr.Software Engineer with 3+ years experience in Java, Spring, Struts, Hibernate, IDM/IAM, and Enterprise Web Application Development.

Website: Java Tutorials Corner

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Read XML File using DOM parser Rating: 5 Reviewed By: Annamalai Thangaraj