Parsing XML In Java Using Java DOM Parser

One of the easiest way to parse XML using Java is to use the DOM parser. The first step is to import the libraries that you need to parse and retrieve the tag values from the XML.

Here’s a sample XML that I have used.

<?xml version="1.0"?>
<employee>
   <emp empno="123">
      <firstname>Rahul</firstname>
      <lastname>Singh</lastname>      
      <salary>8500</salary>
   </emp>
   <emp empno="234">
      <firstname>Sourav</firstname>
      <lastname>Gupta</lastname>      
      <salary>9500</salary>
   </emp>
   <emp empno="345">
      <firstname>Sunil</firstname>
      <lastname>Singh</lastname>      
      <salary>9000</salary>
   </emp>
</employee>

The main class that you need for this purpose is the DocumentBuilderFactory class. Once you have imported this class, all you need to do is create a DocumentBuilder object. After that, you can parse XML as a variety of input sources which includes InputStreams, Files, URLs as well as SAX InputSources.

DocumentBuilderFactory newDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder newDocumentBuilder = newDocumentBuilderFactory.newDocumentBuilder();

Once you have done this, the next step is to create a Document object and storing the parsed XML within it. The next thing that you need to do is normalize it. The normalization is done so that one tag is interpreted as one element. For more information on why you should normalize data, click here.

Document newDocument = newDocumentBuilder.parse(sampleXML);
newDocument.getDocumentElement().normalize();

After this next step is to extract the actual values from the XML tags. For that, we can use a NodeList and Element class.

Here’s the complete code.

package javadomparserxml;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 * @author Lahaul Seth
 */
public class JavaDomParserXML {

    public static void main(String[] args) {
        
        try {
            File sampleXML = new File("sample.xml");
            DocumentBuilderFactory newDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder newDocumentBuilder = newDocumentBuilderFactory.newDocumentBuilder();
            Document newDocument = newDocumentBuilder.parse(sampleXML);
            newDocument.getDocumentElement().normalize();
            System.out.println("Root Tag : " + newDocument.getDocumentElement().getNodeName());
            System.out.println();
            NodeList newNodeList = newDocument.getElementsByTagName("emp");            
            for(int counter = 0; counter<newNodeList.getLength(); counter++) 
            {
                Node newNode = newNodeList.item(counter);                
                if(newNode.getNodeType() == Node.ELEMENT_NODE) 
                {
                    Element newElement = (Element) newNode;
                    System.out.println("Employee No. : " + newElement.getAttribute("empno"));
                    System.out.println("First Name : " + newElement.getElementsByTagName("firstname").item(0).getTextContent());
                    System.out.println("Last Name : " + newElement.getElementsByTagName("lastname").item(0).getTextContent());
                    System.out.println("Salary : " + newElement.getElementsByTagName("salary").item(0).getTextContent()); 
                    System.out.println();
                }          
            }                         
        }
        catch (Exception e) 
        {
            e.printStackTrace();            
        }       
    }    
}

Here’s how the output will look like.

javadomparser2

That’s it for this tutorial. Stay tuned for more tutorials on web and application development.

Got something to share ? Please do so in the comment section. You can also subscribe to our newsletter using the form below to get updates as soon as they’re published.

Share this post :
Lahaul Seth

Lahaul Seth

Software Engineer, Open Source Enthusiast & Blogger - I also own Lion Blogger Tech which focuses on Open Source, UI, UX, Technology.

I also own Open Source Software News which is a curation site for Open Source Technology News & Updates.

Also check out my personal site.