You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by James Richardson <ja...@db.com> on 2001/05/31 19:05:01 UTC

Streaming XML Parsing - next document problem


Also, When I send in an xml document like this:

<!-- SENT(09:12:57.130) -->
<FinHdr>
    <Instrument Act="Subscribe" Dest=":Instrument:">
        <Exchange>Test</Exchange>
    </Instrument>
</FinHdr>

<!-- SENT(09:12:57.131) -->
<FinHdr>
    <InstrumentList Act="Subscribe" Dest=":InstrumentList:"></InstrumentList>
</FinHdr>

After the first </FinHdr>, I get another startDocument() callback ( this is SAX, reading a stream of input, as per the FAQ ), I get the following exception:

org.xml.sax.SAXParseException: The root element is required in a well-formed document.
        at java.lang.Throwable.fillInStackTrace(Native Method)
        at java.lang.Throwable.fillInStackTrace(Compiled Code)
        at java.lang.Throwable.<init>(Compiled Code)
        at java.lang.Exception.<init>(Exception.java:42)
        at org.xml.sax.SAXException.<init>(SAXException.java:45)
        at org.xml.sax.SAXParseException.<init>(SAXParseException.java:56)
        at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1150)
        at org.apache.xerces.framework.XMLDocumentScanner.reportFatalXMLError(XMLDocumentScanner.java:626)
        at org.apache.xerces.framework.XMLDocumentScanner$XMLDeclDispatcher.endOfInput(XMLDocumentScanner.java:846)
        at org.apache.xerces.framework.XMLDocumentScanner.endOfInput(XMLDocumentScanner.java:418)
        at org.apache.xerces.validators.common.XMLValidator.sendEndOfInputNotifications(XMLValidator.java:698)
        at org.apache.xerces.readers.DefaultEntityHandler.changeReaders(DefaultEntityHandler.java:1026)
        at org.apache.xerces.readers.XMLEntityReader.changeReaders(XMLEntityReader.java:168)
        at org.apache.xerces.readers.StreamingCharReader.changeReaders(Compiled Code)
        at org.apache.xerces.readers.StreamingCharReader.lookingAtChar(Compiled Code)
        at org.apache.xerces.framework.XMLDocumentScanner$XMLDeclDispatcher.dispatch(XMLDocumentScanner.java:742)
        at org.apache.xerces.framework.XMLDocumentScanner.parseSome(Compiled Code)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1035)
        at com.db.ged.ovgw.main.OVStreamReader.parseInput(Compiled Code)
        at com.db.ged.ovgw.main.OVStreamReader.main(OVStreamReader.java:91)

Any pointers? Do I need to reset something after the first 'document' is finished.

I'm not doing any schema validation here... ( well I dont want to be doing any )

I'm initialising/running the parser like this:

           SAXParser parser = (SAXParser) Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
            parser.setContentHandler( new DocProcessor() );
     //StreamingChar factory given in FAQ
            parser.setReaderFactory( new StreamingCharFactory());
            while ( true ) {
                try {
                    parser.parse( new InputSource ( new NoCloseBufferedReader ( new InputStreamReader ( inputStream ) ) ) );
                }
          //SAXParseFinishedException is thrown in endElement callback
                catch ( SAXParseFinishedException e ) {
                    CAT.debug ( "Finished node. Loop for next" );
                }
            }

Thanks so much for any help!

James




--

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Streaming XML Parsing - next document problem

Posted by Andy Clark <an...@apache.org>.
James Richardson wrote:
> org.xml.sax.SAXParseException: The root element is required in a well-formed document.

This is normal and according to the spec. Unfortunately, there
is no defined end to an XML document because any number of PIs
and comments may appear after the close of the root element.
And because of buffering in the parser, even if you reset the
parser, there's no way to re-use the buffer -- so the parser
would consume part of the next document which is not what you
want.

If you can control the streams at both ends, then you can use 
the WrappedOutputStream/WrappedInputStream written as a Xerces2 
sample. The sample solves a common socket streaming problem but 
it could also be applied to your particular problem. Check out 
the code with the following command (the code will be extracted 
to the "io" dir):

  cvs checkout -d io -r xerces_j_2 xml-xerces/java/samples/socket

Or use the WebCVS interface. The JavaDoc in the sample should
be enough to figure out how to use it.

-- 
Andy Clark * IBM, TRL - Japan * andyc@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org