retrieving SAX events

I currently use DOM method to process all query results in Tamino. But a part of these queries does not need complex after treatment. So I would like to use the SAX method to do this job.

Although SAX means “Simple API for XML”, the Tamino SAX API seems to me very complicated. I tried to understand this sample but without success…

Could someone post any simple example ? For instance, how to retrieve a group of document from database and write the resulting XML to standard output ?

Should I write specific DocumentDefaultHandler, ElementDefaultHandler, MessageDefaultHandler… for each document I want to retrieve ?

Could I write only one class to handle standard SAX events (startElement, …) ?

Thanks.

Sollan Ltd.

Hi,

Check out the Java API Migration Cookbook - this includes an example of using SAX based processing with the Tamino API for Java.

HTH, Stuart

Do you want to do anything interesting with the result of the fetch or really “just write the resulting XML to standard output”?

Regardless, you need to do a few things:
1) create a class to represent your documents and/or elements that implements the TSAXElement and TSAXDocument interface. These will be instantiated by your element handler.
2) create an element handler class to handle the sax events - an element handler that extends TSAXElementDefaultHandler. In this class, you will probably need an array (ArrayList) to hold the result set of objects (instances of the things defined in step 1 above); the “getFirstElement()” and “getElementIterator()” will refer to this.

Your element handler will implement the SAX event methods - startElement(), endElement(), and characters() being the most interesting.

Now instantiate an element handler and create a new TSAXObjectModel with it and register that using TXMLObjectModel.register(). Set this as your object model on your TXMLObjectAccessor.

Then when you query with this accessor, your event handler methods will be called, you can instantiate your element instances in startElement(), and add them to the result list in endElement(). The trick is maintaining context when handling even moderately complicated documents.

However, if you just want to spew the XML to stdout, you can just println() the arguements to your element handler methods - but I doubt you’re really writing a program to do this (use a web browser like lynx if that’s really what you want).

I hope this is helpful. The whole sax API is pretty opaque (imo).

Thanks for your help.

As a conclusion, programming with the SAX API seems to be much more complex than using the DOM API. Here are the advantages/disadvantages I found :


SAX API


  • Pros :
    - needs little memory
    - much faster than DOM for the big documents
  • Cons :
    - Complex
    - API is pretty opaque
  • Use cases :
    - for the big documents with a lot of nodes


DOM API

  • Pros :
    - quite simple API
    - very flexible
  • Cons :
    - memory usage
  • Use cases :
    - for small or medium size documents


Experiences and feedbacks are welcome to improve this comparative study.

Sollan Ltd.