Example: Sorting XML

We could of course also have used JDOM...
  
  import org.jdom.*;
  import org.jdom.input.*;
  import org.jdom.output.*;

  import java.util.*;
  import java.io.*;

  public class JDOMSort {
    
      public class RecipeComparator implements Comparator {

          public int compare(Object o1, Object o2) {
              Element e1 = (Element)o1;
              Element e2 = (Element)o2;
              String s1 = getRecipeTitle(e1);
              String s2 = getRecipeTitle(e2);
              return s1.compareTo(s2);
          }
        
          public boolean equals(Object o1, Object o2) {
              Element e1 = (Element)o1;
              Element e2 = (Element)o2;
              String s1 = getRecipeTitle(e1);
              String s2 = getRecipeTitle(e2);
              return s1.equals(s2);
          }
      }

      public static String getRecipeTitle(Element e) {
          String res = null;
        
          if (e.getName().equals("recipe")) {
              List e_children = e.getContent();
              Iterator i = e_children.iterator();
              while (i.hasNext()) {
                  Object o1 = i.next();
                  if (o1 instanceof Element) {
                      Element f = (Element)o1;
                      if (f.getName().equals("title")) {
                          List f_children = f.getContent();
                          Iterator j = f_children.iterator();
                          while (j.hasNext()) {
                              Object o2 = j.next();
                              if (o2 instanceof Text) {
                                  Text t = (Text)o2;
                                  if (res==null) {
                                      res = t.getText();
                                  }
                                  else {
                                      res += t.getText();
                                  }  
                              } 
                          } 
                      }
                  } 
              }
          }
          return res;
      }
    
      public Document sort(Document d) {
          Element[] recipes = null;
        
          // Get array of recipe elements
          Element root = d.getRootElement();
          if (root.getName().equals("collection")) {
              List recipeList = new ArrayList();
              List children = root.getContent();
              Iterator i = children.iterator();
              while (i.hasNext()) {
                  Object o = i.next();
                  if (o instanceof Element) {
                      Element e = (Element)o;
                      if (e.getName().equals("recipe")) {
                          recipeList.add(e.clone());
                      }
                  }
              }
              recipes = 
                (Element[])recipeList.toArray(new Element[recipeList.size()]);
          }

          // Sort the array of recipe elements
          if (recipes!=null) {
              Arrays.sort(recipes, new RecipeComparator());
          }

          // Generate and return a sorted collection
          Element collection = new Element("collection", 
                                           "http://www.brics.dk/ixwt/recipes");
          for (int inx=0; inx<recipes.length; inx++) {
              collection.addContent(recipes[inx]);
          }
          return new Document(collection);
      }


      public static void main(String[] args) {
          JDOMSort s = new JDOMSort();
          Document d = null;

          // Load recipe collection
          try {
              SAXBuilder builder = new SAXBuilder();
              InputStream input = new FileInputStream(new File("recipes.xml"));
              d = builder.build(input);
          }  
          catch (Exception e) {
            e.printStackTrace();
            return;
          }

          // Sort
          d = s.sort(d);

          // Print sorted recipe collection
          XMLOutputter out = new XMLOutputter();        
          String res = out.outputString(d);
        
          // Print result
          System.out.println(res);
      }
  }
      

Compared to the XACT version: