users@jaxb.java.net

Re: Unmarshalling more than one root element from an InputStream.

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 12 Feb 2003 11:25:58 -0800

This is not just a problem of JAXB, but rather this is a problem generic
to XML. XML parser is required to scan the input till the very end so
that it can detect ill-formed XMLs like:

----------------------
<?xml version="1.0"?>
<root />
oops! you can't write text!
----------------------

So even if it hits the end of the root element, it will still try to
read the stream until it hits the end of the stream.


So what you have to do is basically to multiplex one stream to
substreams. You have to implement InputStream by yourself so that it
will signal end of stream at the end of each message.
You can either do that by sending the XML message size first or sending
a special marker. Assuming that your InputStream implementation is
"MultiplexedInputStream", you would write something like:

InputStream masterStream = socket.getInputStream();

while( masterStream is not EOS ) {
    MultiplexedInputStream msgStream =
      new MultiplexedInputStream(masterStream);

    Object o = unmarshaller.unmarshal(msgStream);
    doSomethingWith(o);
}


regards,
--
Kohsuke KAWAGUCHI                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com