0% found this document useful (0 votes)
52 views4 pages

Program: Domparser - Java

The Java program uses DOM parsing to count the number of <name> elements in an XML file called emp.xml, which contains data on 5 employees, and prints the count. It imports classes for DOM parsing and handling exceptions, creates a DocumentBuilder to parse the XML file, gets the number of <name> elements, and prints the count.

Uploaded by

Janani Shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views4 pages

Program: Domparser - Java

The Java program uses DOM parsing to count the number of <name> elements in an XML file called emp.xml, which contains data on 5 employees, and prints the count. It imports classes for DOM parsing and handling exceptions, creates a DocumentBuilder to parse the XML file, gets the number of <name> elements, and prints the count.

Uploaded by

Janani Shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM

Domparser.java
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.File;
public class domparser
{
public static void main(String args[])
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder parser=dbf.newDocumentBuilder();
Document doc=parser.parse("emp.xml");
NodeList li=doc.getElementsByTagName("name");
System.out.println("employee xml document has"+li.getLength()+"employee
elements");
}

catch(ParserConfigurationException pce)
{}
catch(SAXException se)
{}
catch(IOException ioe)
{}
}
}

emp.xml
<?xml version="1.0" encoding="UTF-8"?>
<personnel>
<employee type="permanent">
<name>seagull</name>
<id>3674</id>
<age>34</age>
</employee>
<employee type="contract">
<name>robin</name>
<id>3675</id>
<age>25</age>
</employee>
<employee type="permanent">
<name>cruice</name>
<id>3676</id>

<age>28</age>
</employee>
<employee type="permanent">
<name>se</name>
<id>3893</id>
<age>34</age>
</employee>
<employee type="permanent">
<name>se</name>
<id>3893</id>
<age>34</age>
</employee>
</personnel>

OUTPUT

You might also like