JAVA XML

JavaXml Projects

JavaXml Project 1

JavaXml Examples

JavaXml EXAMPLE

Find Element into XML Document
Previous Home Next

In this program, we will learn to find an element in the specified XML file 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

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: FindElement.java

package r4r;
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import java.io.*;
public class findelement {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter the file name: ");
String str = br.readLine();
File file = new File(str);
if (file.exists()){
DOMParser pars = new DOMParser();
pars.parse(str);
Document docu = pars.getDocument();
System.out.print("Enter element which have to find: ");
String str1 = br.readLine();
NodeList list1 = docu.getElementsByTagName(str1);
if(list1.getLength() == 0){
System.out.println("Element doesn't exist in the " +
str + " Document.");
}
else{
System.out.println("Element occurrs " +
list1.getLength() + " times in the " + str);
}
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
e.getMessage();
}
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: