I need help creating a java code that will allow me to search my xml file by vol
ID: 3530594 • Letter: I
Question
I need help creating a java code that will allow me to search my xml file by volume, number, and title. I already have the program running to the point where it is displaying the information for all three variables. ONLY if all three variables have been entered into the program whenever it runs. NOW All I need is something in this program that will make my program still look up the information based on IF any of the three variables are given. So even if one variable isn't present when the program runs, the program still looks up the information by what variables have been presented. If you need variables let me know.Explanation / Answer
Hey i am showing how to search in a xml file for a particular tag..
Like i will take the example of empoyee-info xml file and search for a particular tag in that file
The employee info file looks like below :
======================================================
<?xml version = "1.0" ?>
<Employee-Detail>
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail>
===============================================================
Now let have the program to search this file and print the value of the tag that we are searching...
This is program will ask for the file name there you give the path to your file name...then you have to enter the name of the element like you have mentioned "volumn / number /title" ..any one of these three then this program will calculate the no of occurance of that tag in side the xml file..In your program you can do any thing in place of finding the no of occurance..so check it out..and if any problem then comment..
The code for the java file goes like this :
======================================================================================
importorg.w3c.dom.*;
importorg.apache.xerces.parsers.DOMParser;
importjava.io.*;
publicclassSearchElement{
publicstaticvoidmain(String[]args){
try{
BufferedReaderbf=newBufferedReader(
newInputStreamReader(System.in));
System.out.print("Enterfilename:");
Stringstr=bf.readLine();
Filefile=newFile(str);
if(file.exists()){
DOMParserparser=newDOMParser();
parser.parse(str);
Documentdoc=parser.getDocument();
System.out.print("Enterelementthathavetocount:");
Stringele=bf.readLine();
NodeListlist=doc.getElementsByTagName(ele);
if(list.getLength()==0){
System.out.println("Elementdoesn'texistinthe"+ str+"Document.");
}
else{
System.out.println("Elementoccurrs"+ list.getLength()+"timesinthe"+str);
}
}
else{
System.out.println("Filenotfound!");
}
}
catch(Exceptione){
e.getMessage();
}
}
}
======================================================================================
And the output will looks like :
===========================================
C:inodxml>javac SearchElement.java
C:inodxml>java SearchElement
Enter file name: Employee-Detail.xml
Enter element that have to count: Emp_name
Element doesn't exist in the Employee-Detail.xml Document.
C:inodxml>java SearchElement
Enter file name: Employee-Detail.xml
Enter element that have to count: Emp_Name
Element occurrs 3 times in the Employee-Detail.xml
===================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.