Simple way to Count Number of XML Elements in Java • Crunchify







* @author









            // Reads text from a character-input stream

            BufferedReader reader=newBufferedReader(newInputStreamReader(;


            // Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.

            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();


            // The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document’s data.

            Document doc=factory.newDocumentBuilder().parse(inputFile);


            // Get input element from user

            System.out.print(“Enter element name: “);



            // Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.

            NodeList nodes=doc.getElementsByTagName(element);

            System.out.println(“nHere you go => Total # of Elements: “+nodes.getLength());







Read about:   xml | Top Loan Mortgage

3 thoughts on “Simple way to Count Number of XML Elements in Java • Crunchify

Leave a Reply

Your email address will not be published.