To Count XML Element

In this section, you will learn to count the elements present in a XML file using DOM APIs.

To Count XML Element


In this section, you will learn to count the elements present in a  XML file using DOM APIs. 

Description of program:

This program helps to count the XML element. It takes a xml file (as a string ) at the console and checks its availability. It parses the xml document using the parse() method. After parsing the XML document it asks for element name which have to count.  Create a  NodeList and use the getElementByTagName() method. The getLength() method counts the occurrences of the specified element. If  the asked element is not available( i.e.. the given element isn't found) it returns 0.

Here is the video insturction "How to count XML elements in Java?"

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>

<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>


Here is the Java File:

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

public class DOMCountElement{
  public static void main(String[] args) {
  try {
  BufferedReader bf = new BufferedReader(
new InputStreamReader(;
  System.out.print("Enter File name: ");
  String xmlFile = bf.readLine();
  File file = new File(xmlFile);
  if (file.exists()){
  DocumentBuilderFactory factory 
   = DocumentBuilderFactory.newInstance
  // Create the builder and parse the file
  Document doc = factory.newDocumentBuilder().parse(xmlFile);
  System.out.print("Enter element name: ");
  String element = bf.readLine();
  NodeList nodes = doc.getElementsByTagName(element);
  System.out.println("xml Document Contains " 
   + nodes.getLength
() "  elements.");
  System.out.print("File not found!");
  catch (Exception ex) {

Download this example.

Output of program:


C:\vinod\xml>java DOMCountElement
Enter File name: Employee-Detail.xml
Enter element name: Emp_Name
xml Document Contains 3 elements.

Download source code of the project in Eclipse Project format

Share on Google+Share on Google+

To Count XML Element

Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: To Count XML Element   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 24, 2011
how to obtain element name

Hi, thanks for your example. but i found some problem to obtain the result, below is my xml sample. <?xml version="1.0" encoding="UTF-8" standalone="no"?> <company> <staff id="1"> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>2000000</salary> <age>29</age> </staff> <staff id="2"> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>1000000</salary> </staff> <staff id="3"> <firstname>Ali</firstname> <lastname>Baba</lastname> <nickname>Alibaba</nickname> <salary>199000</salary> <age>40</age> </staff> <manager id="1"> <firstname>Ranov</firstname> <lastname>Baba</lastname> </manager> <manager id="2"> <firstname>Rambo</firstname> <lastname>Baba</lastname> </manager> </company> My objective is to obtain: 1. All Element name inside the "company", in this case are "staff" & "manager" 2. Total Element inside the "company", which the result is 2 ("staff" & "manager"). Thanks for your help.