Mega Code Archive

 
Categories / Java / XML
 

Extends DefaultHandler to create a XML binding

import java.io.File; import java.util.ArrayList; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; public class SAXSample {   public static void main(String[] args) throws Exception {     File file = new File("book.xml");     SAXParserFactory factory = SAXParserFactory.newInstance();     MyHandler handler = new MyHandler();     SAXParser saxParser = factory.newSAXParser();     saxParser.parse(file, handler);     SAXBooks books = handler.getBooks();     for (int i = 0; i < books.getBookSize(); i++) {       SAXBook book = books.getBook(i);       System.out.println(book);     }   } } class MyHandler extends DefaultHandler {   private SAXBooks books;   private boolean readingAuthor;   private boolean readingTitle;   private boolean readingPrice;   public SAXBooks getBooks() {     return this.books;   }   public void startElement(String uri, String localName, String qName, Attributes attributes) {     if (qName.equalsIgnoreCase("books")) {       books = new SAXBooks();     } else if (qName.equalsIgnoreCase("book")) {       SAXBook book = new SAXBook();       for (int i = 0; i < attributes.getLength(); i++) {         if (attributes.getQName(i).equalsIgnoreCase("category")) {           book.setCategory(attributes.getValue(i));         }       }       books.addBook(book);     } else if (qName.equalsIgnoreCase("author")) {       this.readingAuthor = true;     } else if (qName.equalsIgnoreCase("title")) {       this.readingTitle = true;     } else if (qName.equalsIgnoreCase("price")) {       this.readingPrice = true;     } else {       System.out.println("Unknown element: " + qName);     }   }   public void startDocument() {   }   public void endDocument() {   }   public void characters(char[] ch, int start, int length) {     String chars = new String(ch, start, length).trim();     if (chars.length() == 0) {       return;     }     SAXBook book = books.getLastBook();     if (readingAuthor) {       book.setAuthor(chars);     } else if (readingTitle) {       book.setTitle(chars);     } else if (readingPrice) {       book.setPrice(Float.parseFloat(chars));     }   }   public void endElement(String uri, String localName, String qName) {     if (qName.equalsIgnoreCase("author")) {       this.readingAuthor = false;     } else if (qName.equalsIgnoreCase("title")) {       this.readingTitle = false;     } else if (qName.equalsIgnoreCase("price")) {       this.readingPrice = false;     }   } } class SAXBook {   private String title;   private String author;   private String category;   private float price;   public SAXBook() {   }   public SAXBook(String title, String author, String category, float price) {     this.title = title;     this.author = author;     this.category = category;     this.price = price;   }   public String getTitle() {     return this.title;   }   public void setTitle(String title) {     this.title = title;   }   public String getAuthor() {     return this.author;   }   public void setAuthor(String author) {     this.author = author;   }   public String getCategory() {     return this.category;   }   public void setCategory(String category) {     this.category = category;   }   public float getPrice() {     return this.price;   }   public void setPrice(float price) {     this.price = price;   }   public String toString() {     return "Book: " + title + ", " + category + ", " + author + ", " + price;   } } class SAXBooks {   private ArrayList<SAXBook> bookList = new ArrayList<SAXBook>();   public SAXBooks() {   }   public void addBook(SAXBook book) {     this.bookList.add(book);   }   public SAXBook getBook(int index) {     if (index >= bookList.size()) {       return null;     }     return (SAXBook) bookList.get(index);   }   public SAXBook getLastBook() {     return this.getBook(this.getBookSize() - 1);   }   public int getBookSize() {     return bookList.size();   } } <books>    <book category="fiction">      <title>title 1</title>      <author>Tim Lahaye</author>      <price>14.95</price>    </book>    <book category="science fiction">      <title>title 2</title>      <author>Tim Lahaye</author>      <price>14.95</price>    </book>  </books>