Java sax parser example code

SAX (Simple API for XML) is an event-based sequential access parser API developed by the XML-DEV mailing list for XML documents. SAX provides a mechanism for reading data from an XML document that is an alternative to that provided by the Document Object Model (DOM). Where the DOM operates on the document as a whole, SAX parsers operate on each piece of the XML document sequentially.

SAX parsers have certain benefits over DOM-style parsers. The quantity of memory that a SAX parser must use in order to function is typically much smaller than that of a DOM parser. DOM parsers must have the entire tree in memory before any processing can begin, so the amount of memory used by a DOM parser depends entirely on the size of the input data. The memory footprint of a SAX parser, by contrast, is based only on the maximum depth of the XML file (the maximum depth of the XML tree) and the maximum data stored in XML attributes on a single XML element. Both of these are always smaller than the size of the parsed tree itself.

Because of the event-driven nature of SAX, processing documents can often be faster than DOM-style parsers. Memory allocation takes time, so the larger memory footprint of the DOM is also a performance issue.

Due to the nature of DOM, streamed reading from disk is impossible. Processing XML documents larger than main memory is also impossible with DOM parsers but can be done with SAX parsers.

The following Java code parses a sample Order.xml file

Click here for a copy of the Order.xml file used in the sample code program to save the Order Information

package com.as400samplecode;

import java.util.ArrayList;

public class Order {
    String orderType = null;
    String orderID = null;
    String orderDate = null;
    String custNumber = null;
    String custPONumber = null;
    String orderTotal = null;
    ArrayList<OrderDetail> orderDetail = new ArrayList<OrderDetail>();
    public String getOrderType() {
        return orderType;
    public void setOrderType(String orderType) {
        this.orderType = orderType;
    public String getOrderID() {
        return orderID;
    public void setOrderID(String orderID) {
        this.orderID = orderID;
    public String getOrderDate() {
        return orderDate;
    public void setOrderDate(String orderDate) {
        this.orderDate = orderDate;
    public String getCustNumber() {
        return custNumber;
    public void setCustNumber(String custNumber) {
        this.custNumber = custNumber;
    public String getCustPONumber() {
        return custPONumber;
    public void setCustPONumber(String custPONumber) {
        this.custPONumber = custPONumber;
    public String getOrderTotal() {
        return orderTotal;
    public void setOrderTotal(String orderTotal) {
        this.orderTotal = orderTotal;
    public ArrayList<OrderDetail> getOrderDetail() {
        return orderDetail;
    public void setOrderDetail(ArrayList<OrderDetail> orderDetail) {
        this.orderDetail = orderDetail;

} program to save the Order Detail Information

package com.as400samplecode;

public class OrderDetail {
    String orderlineID = null;
    String itemID = null;
    String itemDescription = null;
    String quantity = null;
    String price = null;
    public String getOrderlineID() {
        return orderlineID;
    public void setOrderlineID(String orderlineID) {
        this.orderlineID = orderlineID;
    public String getItemID() {
        return itemID;
    public void setItemID(String itemID) {
        this.itemID = itemID;
    public String getItemDescription() {
        return itemDescription;
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    public String getQuantity() {
        return quantity;
    public void setQuantity(String quantity) {
        this.quantity = quantity;
    public String getPrice() {
        return price;
    public void setPrice(String price) {
        this.price = price;

Click here for next Chapter

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.