سؤال

I'm reading in large XLSX workbooks with multiple sheets. In one of the sheets, I just want the value in A1. The sheet could contain vast amounts of other data. Is it possible to abort the processing of the rest of the sheet (for time consideration) after I've obtained the one value I want?

Here's where I'd like to break processing:

    private static class shValidationsHandler extends DefaultHandler {  
        //The first cell (A1) holds the only value I want
        private static String lastVal = "";
        public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
            lastVal = "";
        }
        public void endElement(String uri, String localName, String name)
            throws SAXException {
            if ( name.equals("v") && sCutOff.length() < 1 ) {
                sCutOff = lastVal;  //<<==Can stop processing now==>>
                //<<=========This is what I added
                SAXException oFin = new SAXException("Finished");
                StackTraceElement[] oStack = new StackTraceElement[1];
                oStack[0] = new StackTraceElement("","","",0);
                oFin.setStackTrace(oStack);
                throw oFin;
                //<<==========End of what I added

            }
        }
        public void characters(char[] ch, int start, int length)
            throws SAXException {
            lastVal += new String(ch, start, length);
        }
    }

Does anyone know if this is possible?

هل كانت مفيدة؟

المحلول

You can do it the same way you'd halt parsing in a normal SAX parser - create a subclass of SAXException (ie, something like HaltProcessingException) and then throw it whenever you are done processing - for example, after sCutOff = lastVal;.

Then your code that invokes that handler (probably something like parser.parse()) should catch that specific exception and log that you've finished processing and suppress the exception.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top