How to 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 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.

ReadXMLFile.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 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
     .newInstance();
   DocumentBuilder builder = builderFactory.newDocumentBuilder();
   Document document = builder.parse(xmlFile);
   document.getDocumentElement().normalize();
   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("***********************");
    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)
         .getTextContent());
     System.out.println("Last Name : "
       + element.getElementsByTagName("lastname").item(0)
         .getTextContent());
     System.out.println("Department : "
       + element.getElementsByTagName("department")
         .item(0).getTextContent());
     System.out.println("***********************");
    }

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

 }

}

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

Shiva
Kumar
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”

student.xml

<?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>

Read XML Output

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
**********************


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. Java DOM XML Parser
  4. How to create XML file using DOM Parser
  5. How to Modify XML file using DOM Parser
  6. How to read simple XML file using DOM Parser

Be the first to comment

Leave a Reply