Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

H - Sorting an xml list then writing to the xml file in its sorted state.

843834Apr 17 2002 — edited May 14 2002
I am working with jsp. I have written an acceptable reader/writer page that iterrates through a list of elements. Applies changes where needed. The problem is: I now want to sort the xml list and save to the xml file, the list in its altered (sorted) state. I have managed to write a comparator class, and this helps sort the list as required, however when I write the xml file, it is not saved sorted as desired.

I am using SAXBuilder.

Just below is a snippet of the jsp page that will iterate through, sort and attempt to write to the xml file.

Below this you will find the code to my Comparator, also for those people who are struggling to understand the list sorting process as I was. (you NEED to write a comparitor class there is no greater alternative)
//this is one of the locations where I have tried sorting the list, there is another below.
Collections.sort(promolistContents, new ElementParameterComparator()); 
		
		while(promolistContentsIterator.hasNext()){
			promolistElement = (Element) promolistContentsIterator.next();

			if(id.equals(promolistElement.getAttributeValue("id"))){
//apply changes to the element
				promolistElement.setAttribute(new Attribute("id",id));
				promolistElement.setAttribute(new Attribute("title",promoTitle));
				promolistElement.setAttribute(new Attribute("dateday",promoDateDay));
				promolistElement.setAttribute(new Attribute("datemonth",promoDateMonth));
				promolistElement.setAttribute(new Attribute("dateyear",promoDateYear));				
				promolistElement.setAttribute(new Attribute("covercharge",promoCovercharge));
				promolistElement.setAttribute(new Attribute("openinghours",promoOpeninghours));
				promolistElement.setText(promoText);

				try {
// I have tried putting the sort code here too, it makes no difference.
//Collections.sort(promolistContents, new ElementParameterComparator()); 

					XMLOutputter outputter = new XMLOutputter("", true);
					outputter.output(promolist, System.out);
					outputter.setTextNormalize(true);
					FileWriter writer = new FileWriter(getServletContext().getRealPath(xmlFileToWrite));
					outputter.output(promolist, writer);
					writer.close();
					success = true;					
				
				} catch (java.io.IOException e) {
					e.printStackTrace();
				}					
			}
		}		
This is the code to the comparitor that helps sort the list.
For those of you who just want to write a jsp page (and do things the hard way like me) that sorts a list, I don't think that you can get arround having to write a class and compiling it.

For the record, this also contains some code which sorts using the Calendar class in the comparator, as opposed to the Date class which, I have found many of the features are becomming depreciated.

Like I mentioned before, I had to do a bit of research before I figgured out how to sort with Calendar, but it was tempmpting to use the old date methods.
package com.altitude;
import java.util.*;
import java.lang.*;
import java.lang.Integer;
import java.lang.Object;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;


public class ElementParameterComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    Element e1 = (Element)o1;
    Element e2 = (Element)o2;
    int returnInt = 0;
    Calendar date1 = Calendar.getInstance();
    Calendar date2 = Calendar.getInstance();

    date1.set(java.lang.Integer.parseInt((String) e1.getAttributeValue("dateyear")), java.lang.Integer.parseInt( (String) e1.getAttributeValue("datemonth")), java.lang.Integer.parseInt( (String) e1.getAttributeValue("dateday")));
    date2.set(java.lang.Integer.parseInt((String) e2.getAttributeValue("dateyear")), java.lang.Integer.parseInt( (String) e2.getAttributeValue("datemonth")), java.lang.Integer.parseInt( (String) e2.getAttributeValue("dateday")));

    if (date1.before(date2))
      returnInt = -1;
    if (date1.equals(date2))
      returnInt = 0;
    if (date1.after(date2))
      returnInt = 1;

      return(returnInt);
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 11 2002
Added on Apr 17 2002
4 comments
508 views