This will load the catalog from the supplied XML file, break
the catalog up by category, and write out new, categorized
XML sub-catalogs.
// Convert from XML to Java
FileInputStream fis = new FileInputStream(catalogFile);
Catalog catalog = new Catalog();
try {
catalog = Catalog.unmarshal(fis);
} finally {
fis.close();
}
// Create new catalogs for the different categories
Catalog fingerpickingCatalog = new Catalog();
Catalog flatpickingCatalog = new Catalog();
Catalog mandolinCatalog = new Catalog();
List items = catalog.getItem();
for (Iterator i = items.iterator(); i.hasNext(); ) {
Item item = (Item)i.next();
String teacher = item.getTeacher();
if ((teacher.equals("Doc Watson")) ||
(teacher.equals("Steve Kaufman"))) {
flatpickingCatalog.getItem().add(item);
} else if (teacher.equals("David Wilcox")) {
fingerpickingCatalog.getItem().add(item);
} else if ((teacher.equals("Sam Bush")) ||
(teacher.equals("Chris Thile"))) {
mandolinCatalog.getItem().add(item);
}
}
// Write back out to XML
FileOutputStream fingerOutput =
new FileOutputStream(new File("fingerpickingCatalog.xml"));
FileOutputStream flatpickOutput =
new FileOutputStream(new File("flatpickingCatalog.xml"));
FileOutputStream mandolinOutput =
new FileOutputStream(new File("mandolinCatalog.xml"));
try {
// Validate the catalogs
fingerpickingCatalog.validate();
flatpickingCatalog.validate();
mandolinCatalog.validate();
// Output the catalogs
fingerpickingCatalog.marshal(fingerOutput);
flatpickingCatalog.marshal(flatpickOutput);
mandolinCatalog.marshal(mandolinOutput);
} finally {
fingerOutput.close();
flatpickOutput.close();
mandolinOutput.close();
}