0% found this document useful (0 votes)
19 views3 pages

Uit Ex 3

The document describes a Java program that implements a DOM parser to read an XML file containing student data. The program first imports necessary libraries and initializes a DocumentBuilder. It then parses the XML file, gets user input for a student ID, and iterates through each student element to check for a matching ID. If a match is found, it prints the student's ID, name, grade, and section. The code sample shows the XML file format and Java class with the main method implementing these steps to successfully parse and search the XML using DOM.

Uploaded by

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

Uit Ex 3

The document describes a Java program that implements a DOM parser to read an XML file containing student data. The program first imports necessary libraries and initializes a DocumentBuilder. It then parses the XML file, gets user input for a student ID, and iterates through each student element to check for a matching ID. If a match is found, it prints the student's ID, name, grade, and section. The code sample shows the XML file format and Java class with the main method implementing these steps to successfully parse and search the XML using DOM.

Uploaded by

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

Exercise No: CS18511-USER INTERFACE TECHNOLOGIES LABORATORY Date:

DOM PARSER

AIM: To write a java code to implement DOM parser.

ALGORITHM:

 Start

 import necessary Java libraries: java.io., org.w3c.dom., javax.xml.parsers., java.util.

 Create a Java class named "parser" with a main method.

 Initialize a DocumentBuilderFactory to create a DocumentBuilder.

 Parse the XML file "trials.xml" using the DocumentBuilder and get the root element of
the XML document.

 Ask the user to input a "User id" using the Scanner class.

 Get a list of "student" elements from the XML document.

 Iterate through each "student" element in the list.

 Check if the "id" attribute of the current "student" element matches the user input.

 If there is a match, print the "student ID," "Name," "class," and "percentage" information
for that student.
 Stop.

CODE:
1) Xml file -- “students.xml”
<?xml version="1.0" encoding="UTF-8"?>
<trails>
<student id=”1”>
<name>Person</name>
<grade>10</grade>
<section>A</section>
</student>
</trials>

REGISTER NO: 21272108010104 PAGE NO:


CS18511-USER INTERFACE TECHNOLOGIES LABORATORY

2) javafile—parser.java

public class parser{

public static void main(String[] args) throws Exception {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();


DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(new File("students.xml"));

Element root = document.getDocumentElement();


Scanner sc = new Scanner(System.in);
System.out.println("Enter user id:");
String id = sc.nextLine();

NodeList nlist = document.getElementsByTagName("student");


System.out.println("Root tag"+root.getNodeName());
for(int temp=0;temp<nlist.getLength();temp++){
Node node = nlist.item(temp);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element ele = (Element)node;

if(ele.getAttribute("id").equals(id)){
System.out.println("Student id:"+ele.getAttribute("id"));
System.out.println("Student
name:"+ele.getElementsByTagName("name").item(0).getTextContent());
System.out.println("Student
grade:"+ele.getElementsByTagName("grade").item(0).getTextContent());
System.out.println("Student
section:"+ele.getElementsByTagName("section").item(0).getTextContent());
}

}
}

REGISTER NO: 21272108010101 PAGE NO:


CS18511-USER INTERFACE TECHNOLOGIES LABORATORY

SAMPLE INPUT AND OUTPUT:

RESULT: Thus a java code is written to implement DOM parser successfully.

REGISTER NO: 21272108010101 PAGE NO:

You might also like