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.
ReadXMLFile.java
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
.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

<firstname>Shiva

Kumar</firstname>

 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

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>

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

***********************

0 comments

Post a Comment