Uit Ex 3
Uit Ex 3
DOM PARSER
ALGORITHM:
Start
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.
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>
2) javafile—parser.java
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());
}
}
}