Java Performance Services
Training, Seminars, Benchmarking, Tuning

Java Performance Tuning Course


Chania Crete, May 17-20, 2010


Sun Extreme Learning EXL-2025

Houston, December 1-4,2009
New York, December 8-11, 2009
Washington DC, January 5-8, 2010



San Francisco, January 11-14

Anti-if

I have joined Anti-IF Campaign

Calendar

««Nov 2009»»
SMTWTFS
1234
5
67
891011121314
15161718192021
22232425262728
2930

Performance Anti-Patterns

My Top Tags

                                       

Mailing List

My RSS Feeds








An Overly Simple SAX Parser

posted Friday, 20 April 2007
On of the comments that I get from the parsing benchmarking section of my performance tuning course is that building a SAX based parser is too difficult. While it maybe difficult to write a complex SAX parser with all the bells and whistles, we often don’t need to go this far to get the job done. For example, I often use XML to store initialization or configuration data. Before I continue I should state that this really isn’t about performance.
<brokerage>
  <client id="1" name="aaa">
    <position ticker="ge" quantity="10000" price="50.00"/>
    <position ticker="sunw" quantity="10000" price="40.00"/>
  </client>
</brokerage>

This XML snippet is pretty typical of most of the XML documents that I’ve come across. We can ignore the root element and just process the nested clauses. As you can imagine, the client clause can be repeated many times. In this case the file has been machine generated (which is why some of the values don’t make real world sense). The code to parse this file looks like this.

public class BrokerageDataSource extends DefaultHandler {
    private Exchange exchange;
    private Brokerage brokerage;
    private Client client;

    public BrokerageDataSource( Exchange exchange) {
        this.exchange = exchange;
        this.brokerage = new Brokerage( this.exchange);
    }

    public void startElement(String namespace, String localName, String qName, Attributes atts) {
        if ( "position".equals( localName)) {
            long quantity = Long.parseLong( atts.getValue( "quantity"));
            double price = Double.parseDouble( atts.getValue("price"));
            Position position = new Position(this.exchange,
                        atts.getValue("ticker"), price, quantity);
            this.brokerage.getPortfolioFor( this.client).addPosition(position);
        } else if (localName.equals("client")) {
            this.client = new Client( atts.getValue( "id"), atts.getValue( "name"));
            this.brokerage.addClient( client);
        }
    }

    public Brokerage parseXML(InputSource in) throws Exception {
        ParserAdapter parseAdaptor = new ParserAdapter(
                SAXParserFactory.newInstance().newSAXParser().getParser());
        parseAdaptor.setContentHandler(this);
        parseAdaptor.parse(in);
        return this.brokerage;
    }
}
The two methods to focus on are parseXML and startElement. This represents the most minimal SAX parser you could possibly write in the it really doesn’t even have enough error checking. This is something that I can get away with in this case as the input file is generated. Of course there is more of the interface you may want to implement such as endElement (and other methods) but in many cases I’ve not found this to be really necessary for this type of useage.

Short story, SAX isn't so difficult for simple things and I've found that most things are simple.

tags: