Next Previous Up Contents
Next: Writing VOTables
Up: Reading VOTables
Previous: Read VOTable document structure

6.2.3 Streamed access

If you only need one-shot access to the data in a single TABLE element, you can use instead the streamStarTable method of VOTableBuilder, which effectively turns a stream of bytes containing a VOTable document into a stream of events representing a table's metadata and data. You define how these events are processed by writing an implementation of the TableSink interface. The data are obtained using SAX parsing and not stored, so it should be fast and have a very small memory footprint. Since it bails out as soon as it has transmitted the table it's after, it may even be able to pull table data out of a stream which is not valid XML.

The following code streams a table and prints out the name of the first column and the average of its values (assumed numerical):

    // Set up a class to handle table processing callback events.
    class ColumnReader implements TableSink {

        private long count_;    // number of rows so far
        private double sum_;    // running total of values from first column

        double average_;        // first column average
        String title_;          // first column name

        // Handle metadata by printing out the first column name.
        public void acceptMetadata( StarTable meta ) {
            title_ = meta.getColumnInfo( 0 ).getName();
        }

        // Handle a row by updating running totals.
        public void acceptRow( Object[] row ) {
            sum_ += ((Number) row[ 0 ]).doubleValue();
            count_++;
        }

        // At end-of-table event calculate the average.
        public void endRows() {
            average_ = sum_ / count_;
        }
    };

    // Streams the named file to the sink we have defined, getting the data
    // from the first TABLE element in the file.
    public void summarizeFirstColumn( URL votLocation ) throws IOException {
        ColumnReader reader = new ColumnReader();
        InputStream in = votLocation.openStream();
        new VOTableBuilder().streamStarTable( in, reader, "0" );
        in.close();
        System.out.println( "Column name:    " + reader.title_ );
        System.out.println( "Column average: " + reader.average_ );
    }

Parameters are obtained in streamed access from PARAM and INFO elements in the same way as described in Section 6.2.1.


Next Previous Up Contents
Next: Writing VOTables
Up: Reading VOTables
Previous: Read VOTable document structure

STIL - Starlink Tables Infrastructure Library
Starlink User Note 252
STIL web page: http://www.starlink.ac.uk/stil/
Author email: m.b.taylor@bristol.ac.uk
Starlink: http://www.starlink.ac.uk/