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

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I guess you can use Comparator, ArrayList and Collections utility class to get your work done.

I guess you can use Comparator, ArrayList and Collections utility class to get your work done.

i am trying with arraylist....please share if u have other ideas...

thanks

I guess you can use Comparator, ArrayList and Collections utility class to get your work done.

i do not understand how to use comaparable.i have used arraylist,but cannot implement the comaparable interface. please help guys.

public class Exemel {
	
		
		public Document readXml(){
		File file=new File("C:\\modelApis\\example.xml");
		DocumentBuilderFactory DocBF=DocumentBuilderFactory.newInstance();
		DocumentBuilder DocB=null;
		try {
			DocB = DocBF.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document doc=null;
		try {
			doc = DocB.parse(file);
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		return doc;
		}
		
		public ArrayList createLOB(Document doc){
		ArrayList empList=new ArrayList();
		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);
				 
				 Exemel emp=new Exemel();
				 empList.add(emp);
				 
			 }
				 }
		 return empList;
		 }
		
		
		public static void main(String[] args)  {
			
			Exemel xml=new Exemel();
			Document doc=xml.readXml();
			ArrayList eList=xml.createLOB(doc);
			
			for(int i=0;i<eList.size();i++){
				System.out.println(eList.get(i));
		}
	}
}

Edited 6 Years Ago by soumya_mjmder: n/a

cannot implement the comaparable interface

Where is your attempt to use the interface? It only has one method.
Have you ever used any interface?

Where is your attempt to use the interface? It only has one method.
Have you ever used any interface?

public class Exemel  {
	
		
		public Document readXml(){
		File file=new File("C:\\modelApis\\example.xml");
		DocumentBuilderFactory DocBF=DocumentBuilderFactory.newInstance();
		DocumentBuilder DocB=null;
		try {
			DocB = DocBF.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document doc=null;
		try {
			doc = DocB.parse(file);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return doc;
		}
		
		
		public List createLOB(Document doc){
			ArrayList empList=new ArrayList();
			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);
					 
					 Exemel emp=new Exemel();
					 empList.add(emp);
					 
				
					// Collections.sort(empList,new EmpSort());
				 }
				 List list=Collections.synchronizedList(empList);
				 Collections.sort(list,new EmpSort());
			 }		
			 return empList;		 
		}
		
		public static void main(String[] args)  {			
			Exemel xml=new Exemel();
			Document doc=xml.readXml();				
			List eList=xml.createLOB(doc);
			
			//Collections.sort(eList,new EmpSort());
			for(int i=0;i<eList.size();i++){
				System.out.println(eList.get(i));
		}
	}
}






public class EmpSort implements Comparator<Exemel> {
	
	         public int compare(Exemel E1, Exemel E2) {
	
		return 0;		
	}

sorry..forgt not providing the interface implementation..
the problem is that here i do not understand how i will do the sorting based on the employee id.
sorry for the trouble...
thanx..

Some sort() methods take a Comparator as an arg.
Look in the API doc for the classes that use that version of the sort() method.

Some sort() methods take a Comparator as an arg.
Look in the API doc for the classes that use that version of the sort() method.

public class EmpSort implements Comparator {
	
	
	private int count=0;
	

	public int compare(Object E1, Object E2) {
		count++;
		System.out.println("Comparator call " + count);
		
	
	
	
		Element temp1=(Element)E1;
		Element temp2=(Element)E2;
		String EID1=temp1.getAttribute("EmployeeId");
		String EID2=temp2.getAttribute("EmployeeId");
		int intEID1=Integer.parseInt(EID1);
		int intEID2=Integer.parseInt(EID2);

		if(intEID1<intEID2){
			return -1;
		}
		else if(intEID1>intEID2){
			return 1;
		}
		else{ return 0;}

		
	}	
}

> This might also work

Generally not recommended unless the range of values is known in advance and is small (i.e. v1 + v2 < Integer.MAX_VALUE). This is because the moment the difference between the two exceeds the range allowed for integers, the value of the expression overflows giving false results. The way OP has tackled the issue is as good as it goes.

I didn't think that the difference between two positive integers would exceed the max value.

I never mentioned positive integers; 2147483646 - (-2147483644) exceeds the max value and hence overflows. Of course, the problem won't arise as long as negative ID's are not allowed (but are currently allowed). My point was that comparing numeric values should be dealt with caution.

Edited 6 Years Ago by ~s.o.s~: n/a

This question has already been answered. Start a new discussion instead.