import org.xml.sax.* ; import java.util.* ; public class LibraryCreator implements ContentHandler { private Map library ; private Book book ; private String isbn ; private List authors ; private StringBuffer buffer ; public LibraryCreator( Map library ) { this.library = library ; } public void startElement( String namespaceURI , String localName , String qualifiedName , Attributes attributes ) throws SAXException { buffer = new StringBuffer(); if( qualifiedName.equals( "book" ) ) { isbn = attributes.getValue( "isbn" ); authors = new ArrayList(); book = new Book(); book.setIsbn( isbn ); } } public void characters( char[] text , int start , int length ) throws SAXException { buffer.append( text , start , length ); } public void endElement( String namespaceURI , String localName , String qualifiedName ) throws SAXException { String value = buffer.toString().trim(); if( qualifiedName.equals( "title" ) ) { book.setTitle( value ); } else if( qualifiedName.equals( "sub-title" ) ) { book.setSubTitle( value ); } else if( qualifiedName.equals( "author" ) ) { authors.add( value ); } else if( qualifiedName.equals( "publisher" ) ) { book.setPublisher( value ); } else if( qualifiedName.equals( "book" ) ) { book.setAuthors( authors ); library.put( isbn , book ); } } public void setDocumentLocator( Locator locator ) {} public void startDocument() throws SAXException {} public void endDocument() throws SAXException {} public void startPrefixMapping( String prefix , String uri ) throws SAXException {} public void endPrefixMapping( String prefix ) throws SAXException {} public void ignorableWhitespace( char[] text , int start , int length ) throws SAXException {} public void processingInstruction( String target , String data ) throws SAXException {} public void skippedEntity( String name ) throws SAXException {} }