Zoo tutorials: [ SQL | Java | Linux | XML ]

A Gentle Introduction to xml

Introducing SAX
With answers

SAX is a "Simple API for XML" - an API is "Application Programmer Interface". SAX allows us to write programs that process XML documents a bit at a time. SAX programs may be more difficult to follow than DOM programs - but they will perform well with the large XML documents that leave DOM thrashing.

Documentation

Question 1: startElement and startDocument and endDocument

The NodeCounter class is an implementation of ContentHandler. We override three methods, startDocument, startElement endDocument. Each of these methods gets called when the parser reaches the corresponding part of the document.

<?xml version="1.0" encoding="UTF-8" ?>
<stock andrew="was here">
  <item price="50" legend="Pr-Burger" BarCode="E1" />
  <item price="15" legend="Crisp S+V" BarCode="E5" />
  <item price="15" legend="Crisp C+O" BarCode="E6" />
  <item price="50" legend="Flat Cola" BarCode="E7" />
</stock>
  • Examine the program and try it out. The output should give the total number of nodes in the input file.
  • Include the line System.out.println(atts.getValue("BarCode")); in the startElement method. This should display the BarCode value for each node as it is processed. Notice value displayed for the stock node which has no such attribute.

Content Handler



Java SAX


Question 2: localName

The parameter localName gives the name of the ndoe - with no namespace information.

  • Change the code so that it only counts and prints nodes if the localName is equal to "item". There should be 4 of them.
  • Change the code so that it prints item number, legend and price on one line for each item. The output should look like:
    1 Pr-Burger 50
    2 Crisp S+V 15
    3 Crisp C+O 15
    4 Flat Cola 50
    

Content Handler



Java SAX