xerces-j and Socket Streams

xerces-j and Socket Streams

Post by Gautham K. Kudv » Wed, 23 Aug 2000 04:00:00



I've just started working with xerces-j and have written a couple of simple
apps that send xml messages to each other on a socket. The problem is that
the SAX parser that uses the socket input-stream hangs waiting for more
input even after it has reached the end of the document. This appears to be
a buffering problem within the parser, since I can see the entire document
contents if I replace the parser with some dummy "echo to console" type
code. The receiver works ok if I close the socket on the sender side, but
then the sender will no longer be able to receive any data from the other
end. I have looked at the Apache FAQ (including creating the
StreamingCharFactory class) but nothing seems to help. I have also tried
puting a whole lot of newlines from the sender side to fill the parser
buffer. That seems to make the parser go a little further (based on
diagnostic output from the document handler methods in my DefaultHandler
derived class). But after a while, the parser simply ignores the newlines
and adding more doesn't help, i.e. my handler's endDocument() method never
gets hit.

Thanks much in advance for any pointers

Gautham Kudva
CBIOS Inc.

 
 
 

xerces-j and Socket Streams

Post by Alain Ketterli » Thu, 24 Aug 2000 04:00:00



Quote:> I've just started working with xerces-j and have written a couple of
> simple apps that send xml messages to each other on a socket. The
> problem is that the SAX parser that uses the socket input-stream
> hangs waiting for more input even after it has reached the end of
> the document.

That's because the SAX handlers gets an endDocument() event only when
the input-stream is closed, i.e., never in the case of a socket (well,
only when the socket is closed).

There was a thread about this a few weeks ago. The only solutions
mentionned were:

1) to wrap a small protocol to retrieve the whole document, then parse
   the in-memory string (if your document is small enough)
2) use a wrapper element for the whole session, and make your sax
   handler deal with first-level elements.

(It all comes from the XML rec. which says that the top-level document
element may be followed by any number of comments and/or pis.)

Hope it helps,

-- Alain.

 
 
 

1. Need help parsing XML stream using Xerces in Java

Hi,

I'm trying to write a Jabber client which needs to parse chunks of an XML
file
off of an input stream on the fly.  I've tried to use the SAX API from
Apache's Xerces,
but with little luck.  I'd be most greatful if someone could tell me what's
wrong
with my Java code below, and even more greatful if someone has a good
working
example of code that does what I want.

At first I tried the following:

public class JabberXMLHandler extends DefaultHandler
{
    ...

    public void parseIt(InputStream stream)
    {
        SAXParser parser = new SAXParser();
        parser.setContentHandler(this);
        parser.parse(new InputSource(stream));
    }

Then after reading a document on Xerces' website which mentions that
the parser buffers the entire document before parsing, I followed their
advice and changed the above method to do the following... it at least gave
me an event for the start of the document, didn't appear to parse any
further.

    public void parseIt(InputStream stream)
    {
        SAXParser parser = new SAXParser();
        parser.setContentHandler(this);

        // tell the parser to use streaming char readers
        parser.setReaderFactory(new StreamingCharFactory());

        InputSource source = new InputSource(new InputStreamReader(stream));
        parser.parse(source);
    }

2. Console login

3. Xerces C++: streaming out the xml doc

4. cam corder

5. streaming XML with xerces SAXParser?

6. Microsoft Security Bulletin MS03-022 - 822343

7. xerces-c and streams

8. BRP Files

9. Problem using 'Output Stream' command property to retrieve data stream from stored proc

10. XML over sockets

11. Reading XML from Socket doesn't work, please help

12. saxParser doesn't work with a socket's inputStream

13. XML transfers over sockets???