Read XML file using DOM parser

This demo will show how to read XML file using DOM parser . The Document Object Model provides APIs that let you create, read, modify, delete, and rearrange nodes. Also please be aware DOM load object tree to the heap memory before parsing it. For small XML document DOM is good choice but if you XML document is big then you probably look for SAX instead of DOM which does occupy space in memory. Next demo will show you how to read XMl file using SAX parser.

Below is ReadFileUsingDom.xml file which we are going to read using DOM parser:

<?xml version="1.0"?>  
<Persons>  
 <Person id="1">  
  <FirstName>Java</FirstName>  
  <LastName>Honk</LastName>
  <City>Edison</City>  
  <State>NJ</State>  
  <Email>javahonk@javahonk.com</Email>  
  <Phone>1234567890</Phone>  
 </Person>
</Persons>

 

ReadXmlFileUsingDomParser.java :

package com.read;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXmlFileUsingDomParser {

    public static void main(String[] args) {
	readXmlFileUsingDomParser();

    }

    public static void readXmlFileUsingDomParser() {
	try {

	    File xmlFile = new File(
		    "resources/ReadFileUsingDom.xml");
	    DocumentBuilderFactory documentFactory = DocumentBuilderFactory
		    .newInstance();
	    DocumentBuilder documentBuilder = documentFactory
		    .newDocumentBuilder();
	    Document doc = documentBuilder.parse(xmlFile);

	    doc.getDocumentElement().normalize();
	    NodeList nodeList = doc
		    .getElementsByTagName("Person");

	    System.out.println("Root element :"
		    + doc.getDocumentElement()
			    .getNodeName());

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

		System.out.println("\nElement type :"
			+ node.getNodeName());

		if (node.getNodeType() == Node.ELEMENT_NODE) {

		    Element person = (Element) node;

		    System.out.println("Person id : "
			    + person.getAttribute("id"));
		    System.out.println("First Name : "
			    + person.getElementsByTagName(
				    "FirstName").item(0)
				    .getTextContent());
		    System.out.println("Last Name : "
			    + person.getElementsByTagName(
				    "LastName").item(0)
				    .getTextContent());
		    System.out.println("City : "
			    + person.getElementsByTagName(
				    "City").item(0)
				    .getTextContent());
		    System.out.println("State : "
			    + person.getElementsByTagName(
				    "State").item(0)
				    .getTextContent());
		    System.out.println("Email : "
			    + person.getElementsByTagName(
				    "Email").item(0)
				    .getTextContent());
		    System.out.println("Phone : "
			    + person.getElementsByTagName(
				    "Phone").item(0)
				    .getTextContent());

		}
	    }
	} catch (Exception exception) {
	    exception.printStackTrace();
	}
    }

}

 

Output:

Read XML file using DOM parser

Leave a Reply

Your email address will not be published. Required fields are marked *