Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 10 November 2013

Count XML Element using DOM parser


In this tutorials we going to see how to count the element in XML document using DOM parser .
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.dom under JavaXML.
3. Create Java class called CountXMLElement.java under com.javatutorialscorner.xml.dom package.
CountXMLElement.java
01.package com.javatutorialscorner.xml.dom;
02. 
03.import java.io.File;
04.import java.io.IOException;
05. 
06.import javax.xml.parsers.DocumentBuilder;
07.import javax.xml.parsers.DocumentBuilderFactory;
08.import javax.xml.parsers.ParserConfigurationException;
09. 
10.import org.w3c.dom.Document;
11.import org.w3c.dom.NodeList;
12.import org.xml.sax.SAXException;
13. 
14.public class CountXMLElement {
15. 
16. /**
17.  * @param args
18.  */
19. public static void main(String[] args) {
20.  // TODO Auto-generated method stub
21. 
22.  try {
23.   File xmlFile = new File("C:\\jtc\\student.xml");
24.   DocumentBuilderFactory builderFactory = DocumentBuilderFactory
25.     .newInstance();
26.   DocumentBuilder builder = builderFactory.newDocumentBuilder();
27.   Document document = builder.parse(xmlFile);
28.   document.getDocumentElement().normalize();
29.   System.out.println("Root Node :"
30.     + document.getDocumentElement().getNodeName());
31.   NodeList list = document.getElementsByTagName("student");
32.   System.out.println("Number of element under "
33.     + document.getDocumentElement().getNodeName() + " : "
34.     + list.getLength());
35.  } catch (ParserConfigurationException e) {
36.   // TODO Auto-generated catch block
37.   e.printStackTrace();
38.  } catch (SAXException e) {
39.   // TODO Auto-generated catch block
40.   e.printStackTrace();
41.  } catch (IOException e) {
42.   // TODO Auto-generated catch block
43.   e.printStackTrace();
44.  }
45. 
46. }
47. 
48.}

student.xml

01.<?xml version="1.0" encoding="UTF-8" standalone="no"?>
02.<college>
03.<student id="ECE-01">
04.<firstname>Sathish</firstname>
05.<lastname>J</lastname>
06.</student>
07.<student id="ECE-02">
08.<firstname>Mohan</firstname>
09.<lastname>P</lastname>
10.</student>
11.<student id="ECE-03">
12.<firstname>Saravan</firstname>
13.<lastname>P</lastname>
14.</student>
15.<student id="ECE-04">
16.<firstname>Muthu</firstname>
17.<lastname>S</lastname>
18.</student>
19.</college>

Now run the program see the following output in console.

Root Node :college

Number of element under college : 4

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Read XML file using DOM Parser1In this tutorials we going to see how to read the XML document and iterate the nodes one by one using DOM parser 1. Create Project Called JavaXML. 2. Create package called com.javatutorialscorner.xml.dom under JavaXML. 3. Cre… Read More
  • SAX Parser Introduction SAX – Simple API for XML SAX parser work differently with DOM parser.SAX parser is faster and uses less memory than DOM parser.SAX don’t load and XML document into memory and don’t create any object representation of XM… Read More
  • Create XML File using DOM ParserIn this tutorials we going to see how to create the XML document using DOM parser 1. Create Project Called JavaXML. 2. Create package called com.javatutorialscorner.xml.dom under JavaXML. 3. Create Java class called CreateXML… Read More
  • Java StAX IntroductionIn this tutorial we are going to see the Java Streaming API for XML (StAX). The StAX is a streaming Java technology-based, event-driven, pull-parsing API for reading and writing XML documents. StAX enables you to create bidr… Read More
  • Read XML File using DOM parserIn this tutorial we are going to see how to read XML file using DOM Parser. In this example I used to read XML document by Element Name.DOM parser parse the entire XML document and load it into memory.DOM parse the nodes as t… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Count XML Element using DOM parser Rating: 5 Reviewed By: eHowToNow