Class StreamingParser


  • public class StreamingParser
    extends Object
    XML parser capable of streaming.

    Performs the same task as Parser, with the addition that objects are streamed back to the client. Streaming can occur in a number of different modes.

    As an example consider the following gml document:

     <test:TestFeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:gml="http://www.opengis.net/gml"
            xmlns:test="http://www.geotools.org/test"
            xsi:schemaLocation="http://www.geotools.org/test test.xsd">
    
       <gml:featureMember>
          <test:TestFeature fid="0">
                  ...
          </test:TestFeature>
       </gml:featureMember>
    
       <gml:featureMember>
          <test:TestFeature fid="1">
                  ...
          </test:TestFeature>
       </gml:featureMember>
    
       <gml:featureMember>
          <test:TestFeature fid="2">
                  ....
          </test:TestFeature>
       </gml:featureMember>
    
     </test:TestFeatureCollection>
     
    And suppose we want to stream back each feature as it is parsed.

    1. Element Name

    Objects are streamed back when an element of a particular name has been parsed.
        Configuration configuration = new GMLConfiguration();
        QName elementName = new QName( "http://www.geotools.org/test", "TestFeature" );
    
        StreamingParser parser = new StreamingParser( configuration, elementName );
    
        Feature f = null;
        while ( ( f = parser.parse() ) != null ) {
           ...
        }
      

    2. Type

    Objects are streamed back when an element has been parsed into an object of a particular type.
        Configuration configuration = new GMLConfiguration();
        StreamingParser parser = new StreamingParser( configuration, Feature.class );
    
        Feature f = null;
        while ( ( f = parser.parse() ) != null ) {
           ...
        }
      

    3. Xpath Expression

    Objects are streamed back when an element has been parsed which matches a particular xpath expression.
        Configuration configuration = new GMLConfiguration();
        String xpath = "//TestFeature";
        StreamingParser parser = new StreamingParser( configuration, xpath );
    
        Feature f = null;
        while ( ( f = parser.parse() ) != null ) {
           ...
        }
      
    PullParser offers similar functionality out of a pull parser instead of a SAX parser, it's supposed to be better, but won't (yet) cover the entire functionality offered by StreamingParser
    Author:
    Justin Deoliveira, The Open Planning Project
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Object parse()
      Streams the parser to the next element in the instance document which matches the xpath query specified in the contstructor.
      void setEntityResolver​(EntityResolver entityResolver)  
      • Methods inherited from class Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StreamingParser

        public StreamingParser​(Configuration configuration,
                               InputStream input,
                               Class type)
                        throws ParserConfigurationException,
                               SAXException
        Creates a new instance of the type based streaming parser.
        Parameters:
        configuration - Object representing the configuration of the parser.
        input - The input stream representing the instance document to be parsed.
        type - The type of parsed objects to stream back.
        Throws:
        ParserConfigurationException
        SAXException
      • StreamingParser

        public StreamingParser​(Configuration configuration,
                               InputStream input,
                               QName elementName)
                        throws ParserConfigurationException,
                               SAXException
        Creates a new instance of the element name based streaming parser.
        Parameters:
        configuration - Object representing the configuration of the parser.
        input - The input stream representing the instance document to be parsed.
        elementName - The name of elements to stream back.
        Throws:
        ParserConfigurationException
        SAXException
      • StreamingParser

        public StreamingParser​(Configuration configuration,
                               InputStream input,
                               String xpath)
                        throws ParserConfigurationException,
                               SAXException
        Creates a new instance of the xpath based streaming parser.
        Parameters:
        configuration - Object representing the configuration of the parser.
        input - The input stream representing the instance document to be parsed.
        xpath - An xpath expression which dictates how the parser streams objects back to the client.
        Throws:
        ParserConfigurationException
        SAXException
      • StreamingParser

        protected StreamingParser​(Configuration configuration,
                                  InputStream input,
                                  StreamingParserHandler handler)
                           throws ParserConfigurationException,
                                  SAXException
        Internal constructor.
        Throws:
        ParserConfigurationException
        SAXException
    • Method Detail

      • setEntityResolver

        public void setEntityResolver​(EntityResolver entityResolver)
      • parse

        public Object parse()
        Streams the parser to the next element in the instance document which matches the xpath query specified in the contstructor. This method returns null when there are no more objects to stream.
        Returns:
        The next object in the stream, or null if no such object is available.