FileDocCategorySizeDatePackage
CatalogViewer.javaAPI DocExample4952Wed Sep 19 09:50:58 BST 2001javaxml2

CatalogViewer

public class CatalogViewer extends Object

CatalogViewer uses Zeus data binding to read in an XML catalog file and show its contents, as well as adding a new item.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

Provide a static entry point.

        try {
            if (args.length != 1) {
                System.out.println("Usage: java javaxml2.CatalogViewer " +
                    "[XML Catalog Filename]");
                return;
            }

            // Get access to XML catalog
            File catalogFile = new File(args[0]);
            CatalogViewer viewer = new CatalogViewer();
            viewer.view(catalogFile);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    
public voidview(java.io.File catalogFile)

This will read in the catalog from the supplied XML file.

param
catgalogFile File to read catalog from.
throws
IOException - when file access errors occur.

        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);