Parsing XML in Java using DOM

In this post, I will show how to parse a XML content using DOM parser in Java. There are several DOM implementations available for Java, both inbuilt and external library based. Here, I have used inbuilt simple approach.

Following example shows a XML content dynamically created. If it is required to be read from file, then also the process is similar.

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 XmlReader
	public XmlReader()


	public void parse(StringBuffer xml)
		  try {

			  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			  DocumentBuilder db = dbf.newDocumentBuilder();
			  InputStream is = new StringBufferInputStream(xml.toString());
			  Document doc = db.parse(is);
			  System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
			  NodeList nodeLst = doc.getElementsByTagName("book");
			  System.out.println("Book Details: ");

			  for (int s = 0; s < nodeLst.getLength(); s++) {

			    Node fstNode = nodeLst.item(s);

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

			           Element fstElmnt = (Element) fstNode;
			      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("title");
			      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
			      NodeList fstNm = fstNmElmnt.getChildNodes();
			      System.out.println("Title : "  + ((Node) fstNm.item(0)).getNodeValue());
			      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("author");
			      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
			      NodeList lstNm = lstNmElmnt.getChildNodes();
			      System.out.println("Author : " + ((Node) lstNm.item(0)).getNodeValue());

			  } catch (Exception e) {


	public static void main(String args[])
		StringBuffer xml = new StringBuffer();

		xml.append("<?xml version=\"1.0\"?>");
			xml.append("<title>C++ The Complete Reference</title>");
			xml.append("<author>Herbert Schildt</author>");
			xml.append("<title>Advanced Java 2 How to Program</title>");
			xml.append("<author>Deitel and Deitel</author>");
			xml.append("<title>Eclipse: Building Commercial-Quality Plug-ins</title>");
			xml.append("<author>Eric Clayberg, Dan Rubel</author>");

		XmlReader xmlreader = new XmlReader();


First we need a reference to the root element of the document. This is also called document element or the node object. Like any other nodes, we can retrieve its name using getNodeName() method. You can see the method call


in above code.

Next, we can get all of the child nodes of the root element using the getChildNodes() method, which returns a NodeList that includes both the book elements and the text nodes in between them. We can iterate through that list much as we would iterate through any other list, using methods such as getNextSibling(). Each node has a type, which you can test against fields such as TEXT_NODE and ELEMENT_NODE to determine what it is.

If you want to parse the XML content that is in a file, then you can do it with a slight modification. It is shown below.

        File docFile = new File("books.xml");

        Document doc = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(docFile);

6 thoughts on “Parsing XML in Java using DOM

  1. Pingback: Parsing XML in Java using SAX « Developer’s Point

  2. Hi I am trying to parse an xml file in JAVA using DOM. This xml file comes via an external URL. How can I do that? Can u help me by providing some examples

  3. Hi prajwalan,
    Can you tell me how to parse this XML file using DOM?

    i want only 2 nodes a and b in tag.. but when i tried with listOfNodes = doc.getElementsByTagName(“nodes”); I’m getting 14 nodes.. so can u please help me ?
    Thanks in advance


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>