Hi guys..i have a XML:
<?xml version="1.0" ?>
<EmployeeDetails>
<Employee EmployeeName="Johnny" EmployeeId="209007" CompanyName="ABC Inc." CompanyAddress="Bangalore" ContactNo="0000000" />
<Employee EmployeeName="JohnnyDepp" EmployeeId="78452" CompanyName="ABC Inc." CompanyAddress="Bangalore" ContactNo="0000000" />
<Employee EmployeeName="JohnnyStechhino" EmployeeId="45678" CompanyName="ABC Inc." CompanyAddress="Bangalore" ContactNo="0000000" />
<Employee EmployeeName="JohnDoe" EmployeeId="45981" CompanyName="ABC Inc." CompanyAddress="Bangalore" ContactNo="0000000" />
<Employee EmployeeName="Soumyadeep" EmployeeId="12345" CompanyName="ABC Inc." CompanyAddress="Bangalore" ContactNo="0000000" />
</EmployeeDetails>
Now i want to sort the output of this xml based on the EmployeeId attribute:
I have written the code for the output of the xml but cannot understand how to sort it...i need to sort it without using xslt...
public class Exemel {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
File file=new File("C:\\modelApis\\example.xml");
DocumentBuilderFactory DocBF=DocumentBuilderFactory.newInstance();
DocumentBuilder DocB=DocBF.newDocumentBuilder();
Document doc=DocB.parse(file);
Element docEle=doc.getDocumentElement();
NodeList nl=docEle.getElementsByTagName("Employee");
System.out.println("Employee Info:");
if(nl!=null && nl.getLength()>0){
for(int i=0;i<nl.getLength();i++){
Element el=(Element)nl.item(i);
String name=el.getAttribute("EmployeeName");
System.out.println("\nName:"+name);
String empId=el.getAttribute("EmployeeId");
System.out.println("Id:"+empId);
String cmp=el.getAttribute("CompanyName");
System.out.println("Company:"+cmp);
String cmpAdd=el.getAttribute("CompanyAddress");
System.out.println("Address:"+cmpAdd);
String cntNo=el.getAttribute("ContactNo");
System.out.println("Contact:"+cntNo);
}
}
}
}
please help guys...thanks in advance