MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

JAVA XML

JavaXml Projects

JavaXml Project 1

JavaXml Examples

JavaXml EXAMPLE

Fetch all XML Elements
Previous Home Next

In this program, we will be study how to retrieve every elements of the XML file with help of the DOM Application programming Interface. The API provides some methods and constructors which is help to parse the XML file and retrieve all elements.

Here is the XML File: Student_Detail.xml

<?xml version = "1.0" ?>
<Student_Detail>
<Student>
<stu_Id> s-001 </stu_Id>
<stu_Name> samir </stu_Name>
<stu_E-mail> sam1@gmail.com </stu_E-mail>
</Student>
<Student>
<stu_Id> s-002 </stu_Id>
<stu_Name> Ajit </stu_Name>
<stu_E-mail> Ajit2@yahoo.com </stu_E-mail>
</Student>
<Student>
<stu_Id> E-003 </stu_Id>
<stu_Name> Deepti </stu_Name>
<stu_E-mail> Deep3@yahoo.com </stu_E-mail>
</Student>
</Student-Detail>

 

Here is the Java File: Studentrecord.java

package r4r;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class fetchrecord {
public static void main(String[] arg){
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter the XML File name: ");
String str = br.readLine();
File file = new File(str);
if(file.exists()){
DocumentBuilderFactory fact =
DocumentBuilderFactory.newInstance();
DocumentBuilder build = fact.newDocumentBuilder();
Document docu = build.parse(str);
NodeList list1 = docu.getElementsByTagName("*");
System.out.println("XML Elements: ");
for (int i=0; i<list1.getLength(); i++) {
Element ele = (Element)list1.item(i);
System.out.println(ele.getNodeName());
}
}
else{
System.out.print("File not found!");
}
}
catch (Exception e) {
System.exit(1);
}
}
}
Previous Home Next