In this section, you will learn to count the element in
XML document using DOM APIs defined in the org.apache.xerces.parsers.DOMParser
package. Your classpath must contain xercesImpl.jar and
xml-apis.jar files to run this program.
You can download it from Xerces
Description of program: This program takes a file name from the console and checks its availability. If the file exists then DOMParser is created using the org.apache.xerces.parsers.DOMParser package.This object parses the given XML document. It asks the element name and counts its occurence in the xml file. If the given element doesn't exist it displays the '0' element.
Here is the XML File: Employee-Detail.xml
| <?xml version = "1.0" ?> <Employee-Detail> <Employee> <Emp_Id> E-001 </Emp_Id> <Emp_Name> Vinod </Emp_Name> <Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail> </Employee> <Employee> <Emp_Id> E-002 </Emp_Id> <Emp_Name> Amit </Emp_Name> <Emp_E-mail> Amit2@yahoo.com </Emp_E-mail> </Employee> <Employee> <Emp_Id> E-003 </Emp_Id> <Emp_Name> Deepak </Emp_Name> <Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail> </Employee> </Employee-Detail> |
Here is the Java File: CountNodes.java
import org.w3c.dom.*;
|
Output of program:
| C:\vinod\xml>javac CountNodes.java C:\vinod\xml>java CountNodes Enter file name: Employee-Detail.xml Enter element that have to count: Emp_Name Number of nodes: 3 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: To Count The Elements in a XML File
Post your Comment