Is there a Push-based/Non-blocking XML Parser for Java?

前端 未结 7 1769
忘了有多久
忘了有多久 2020-12-24 14:24

I\'m looking for an XML parser that instead of parsing from an InputStream or InputSource will instead allow blocks of text to be pushed into the parser. E.g. I would like t

7条回答
  •  渐次进展
    2020-12-24 15:03

    Adding another answer as this question remains high for relevant Google searches - aalto-xml 0.9.7 (March 2011) has asynchronous XML pasing. This allows you to pass arbitrary sized chunks of a document to continue parsing, and a new StaX event type EVENT_INCOMPLETE to indicate the input buffer is exhausted and the document remains incomplete.

    This is Tatu Salorant's (the author's) example:

         byte[] msg = "Very simple input document!".getBytes();
          AsyncXMLStreamReader asyncReader = new InputFactoryImpl().createAsyncXMLStreamReader();
          final AsyncInputFeeder feeder = asyncReader.getInputFeeder();
          int inputPtr = 0; // as we feed byte at a time
          int type = 0;
    
          do {
            // May need to feed multiple "segments"
            while ((type = asyncReader.next()) == AsyncXMLStreamReader.EVENT_INCOMPLETE) {
              feeder.feedInput(msg, inputPtr++, 1);
              if (inputPtr >= msg.length) { // to indicate end-of-content (important for error handling)
                feeder.endOfInput();
              }
            }
            // and once we have full event, we just dump out event type (for now)
            System.out.println("Got event of type: "+type);
            // could also just copy event as is, using Stax, or do any other normal non-blocking handling:
            // xmlStreamWriter.copyEventFromReader(asyncReader, false);
          } while (type != AsyncXMLStreamReader.END_DOCUMENT);
    

提交回复
热议问题