extends DefaultHandler to create a XML binding : XML Data « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » XML » XML Data 




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[] argsthrows 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 (SAXBookbookList.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> 

   
    
    
  














Related examples in the same category
1.Streaming XML
2.Your won XML binding
3.Parse and format xs:dateTime values
4.Replaces all XML character entities with the character they represent.
5.Sniffed Xml InputStream to find out the declaration and file encoding
6.Whether the given character is a valid XML space.
7.JAXP 1.3 Datatype API
8.Common XML constants.
9.Sniffed Xml Reader
10.Source To InputSource
11.Utility class for working with xml data
12.whether the given 32 bit character is a valid XML 1.1 character.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.