Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

ZwiftBooks and SAX

To understand how an event-based API works, let’s look at how ZwiftBooks can integrate incoming EuroBooks XML into an existing warehouse alert application. Currently, ZwiftBooks takes telephone orders and, prior to validating payment, alerts the warehouse about potential deliveries. For each potential order, the ISBN is entered via a screen display, which triggers custom software that shows the ISBN on a display in the warehouse. The incoming alert sets things in motion so that the book is readied and placed in a special bin for courier pickup only seconds after payment is received.

Since EuroBooks sends its orders electronically as XML, management has proposed using the ISBN from the XML to trigger the warehouse order-alert system. This is an excellent task for an event-based parser such as SAX, since we don’t care about other aspects of the XML—only the ISBN. The choice of SAX over DOM is dictated by the fact that it’s inefficient to construct and traverse an in-memory parsing tree just to locate this one piece of data. SAX lets us quickly locate the ISBN with minimal overhead (see Figure 2).

Figure 2

Figure 2 Data extracted from incoming XML documents can be used to trigger an existing warehouse-alert system.

Listing 1 shows the SAX handler for responding to ISBNs, coming in as attributes, from the incoming XML. To extract the attribute, we need to handle a startElement event because, in SAX, attributes are retrievable from their associated start element. In SAX, the attributes are passed in an Attributes interface that supports the following methods:

  • getLength() returns the number of attributes.
  • getQName(i) obtains the ith attribute name.
  • getValue(i) obtains the ith attribute value.

Listing 1 A SAX handler for extracting ISBN and alerting the warehouse.

 1. import javax.xml.parsers.*;
 2. import org.xml.sax.*;
 3. import org.xml.sax.helpers.*;
 4.
 5.
 6. public class SaxIsbnHandler extends DefaultHandler {
 7.
 8. private int indentation = 0;
 9.
10. public void startElement(String namespaceUri,
11.        String localName,
12.        String qualifiedName,
13.        Attributes attributes)
14.        throws SAXException {
15.
16.  // check if we have a <book>, and if so
17.  // retrieve the isbn attribute
18.  if (localName.equals("book") {
19.   int numAttributes = attributes.getLength();
20.
21.   if (numAttributes > 0) {
22.
23.   // step through each attribute, looking for isbn
24.   for(int i=0; i<numAttributes; i++) {
25.
26.   if (attributes.getQName(i).equals("isbn") ) {
27.    isbnNumber = attributes.getValue(i));
28.    alertWarehouse( isbnNumber );
29.    break;
30.   }
31.
32.   } // end for
33.   }
34.
35.  } // end if
36. }
37.
38. private void alertWarehouse (String isbn) {
39.
40.  // assume warehouse is an output stream
41.  warehouse.println("Pending order for isbn=" + isbn);
42.
43. }
  • + Share This
  • 🔖 Save To Your Account