Sample Programs

Sample java program using DOM Parser


package com.jusfortechies.xml.parsers;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMHandler {

  private static BookStore store = null;

  public static BookStore getStore() {
    return store;
  }

  public static void parseDocument(Document document) {
    // get the root elememt
    Element rootElement = document.getDocumentElement();
    store = new BookStore();

    // get a nodelist of <book> elements
    NodeList nl = rootElement.getElementsByTagName("book");
    if (nl != null && nl.getLength() > 0) {
      for (int i = 0; i < nl.getLength(); i++) {

        // get the Book element
        Element el = (Element) nl.item(i);

        // Create the Book object
        Book book = new Book();

        // Read the category attribute
        book.setCategory(el.getAttribute("category"));

        // Read the <title> element which is child element of book element
        NodeList titles = el.getElementsByTagName("title");
        if (titles != null && titles.getLength() > 0) {
          for (int index = 0; index < titles.getLength(); index++) {
            Element titleElement = (Element) titles.item(index);

            Title bookTitle = new Title();
            bookTitle.setBookTitle(titleElement.getFirstChild()
                .getNodeValue());
            bookTitle
            .setLanguage(titleElement.getAttribute("lang"));

            book.setBookTitle(bookTitle);
          }
        }

        // Read the <year> element which is child element of book element
        NodeList year = el.getElementsByTagName("year");
        if (year != null && year.getLength() > 0) {
          for (int index = 0; index < year.getLength(); index++) {
            book.setYear(((Element) year.item(index))
            .getFirstChild().getNodeValue());
          }
        }

        // Read the <author> element which is child element of book element
        NodeList authors = el.getElementsByTagName("author");
        if (authors != null && authors.getLength() > 0) {
          for (int index = 0; index < authors.getLength(); index++) {
            book.addAuthor(((Element) authors.item(index))
            .getFirstChild().getNodeValue());
          }
        }

        // Read the <price> element which is child element of book element
        NodeList price = el.getElementsByTagName("price");
        if (price != null && price.getLength() > 0) {
          for (int index = 0; index < price.getLength(); index++) {
            book.setPrice(Double.parseDouble(price.item(index)
            .getFirstChild().getNodeValue()));
          }
        }

        store.addBook(book);
        store.addBookToCategory(book.getCategory(), book);
      }
    }
  }


  public static void parseXML(String xmlFileName) {
    // get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {
      // Using factory get an instance of document builder
      DocumentBuilder db = dbf.newDocumentBuilder();

      // parse using builder to get DOM representation of the XML file
      Document dom = db.parse(xmlFileName);

      // Build the object graph.
      DOMHandler.parseDocument(dom);
    } catch (ParserConfigurationException pce) {
      pce.printStackTrace();
    } catch(SAXException se) {
      se.printStackTrace();
    }catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

}

Download the BookStore.java, Book.java and Title.java here.

BookStore.java
Book.java
Title.java



blog comments powered by Disqus