This will read in the catalog from the supplied XML file.
FileReader reader = new FileReader(catalogFile);
StreamSource source = new StreamSource(reader);
// Convert from XML to Java
Unmarshaller unmarshaller = new Unmarshaller();
unmarshaller.setJavaPackage("javaxml2.zeus");
Catalog catalog = (Catalog)unmarshaller.unmarshal(source);
List items = catalog.getItemList();
for (Iterator i = items.iterator(); i.hasNext(); ) {
Item item = (Item)i.next();
String id = item.getId();
System.out.println("Item ID: " + id);
String title = item.getTitle().getValue();
System.out.println("Item Title: " + title);
// Modify an item
if (id.equals("CDZ-SM01")) {
item.getTitle().setValue("Sam Bush Teaches Mandolin " +
"Repertoire and Technique, 2nd edition");
Guest guest = new GuestImpl();
guest.setValue("Bela Fleck");
item.addGuest(guest);
}
}
// Write back out
FileWriter writer = new FileWriter(new File("newCatalog.xml"));
StreamResult result = new StreamResult(writer);
Marshaller marshaller = new Marshaller();
marshaller.marshal(catalog, result);