You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by David Durham <dd...@vailsys.com> on 2006/05/24 01:12:32 UTC

[digester] multiple parses from one inputstream

Hi all,

I'd like to use Digester to parse multiple XML messsages being passed 
from a server to a client.  The parse method hangs until I close the 
server output stream.

Anyway to signal to Digester.parse(InputStream) that it should return 
without having reached the end of an inputstream?  Or do I have chose a 
different parse method?

Basically, I'd like to do something like this in the client:

   ..
     socket = new Socket("localhost", port);

     out = new PrintWriter(socket.getOutputStream(), true);
     in = new BufferedReader(new InputStreamReader(socket.getInputStream());

     String inputLine = null;
     while ((inputLine = stdIn.readLine()) != null) {
       System.out.println("From user: " + inputLine);
       if (inputLine.equals("Bye.")) {
         out.println("Bye.");
         break;
       }
       Digester digester = new Digester();
       digester.setValidating( false );

       digester.addObjectCreate( "test", DigesterTest.class );
       digester.addBeanPropertySetter("test/property", "property");

       DigesterTest test = null;

       try {
          test = (DigesterTest)digester.parse(in);
       } catch (IOException e) {
          e.printStackTrace(System.out);
       } catch (SAXException e) {
          e.printStackTrace(System.out);
       }

       System.out.println(test.getProperty());
       out.println("ready");
     }

     out.close();
     in.close();
     stdIn.close();
     socket.close();


Again, the client hangs on digester.parse until server closes output 
stream.  Thanks,

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] multiple parses from one inputstream

Posted by lu...@free.fr.
Selon Simon Kitching <sk...@apache.org>:


> Digester just calls someXmlParser.parse(). If you can find an xml parser
> that invokes endDocument then returns when it reaches the end of the
> root element then Digester should work fine with that. I'm not aware of
> any such parser though.

I think such a behaviour would be wrong in the general case, as it would break
detection of malformed documents.

There would also be problems restarting the parser for the next message.

The need here seems to have one data stream holding a sequence of independent
XML documents, I would suggest using some kind of wrapper to split the stream
at messages boundaries and providing each message individually.

Luc


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] multiple parses from one inputstream

Posted by Simon Kitching <sk...@apache.org>.
On Tue, 2006-05-23 at 18:12 -0500, David Durham wrote:
> Hi all,
> 
> I'd like to use Digester to parse multiple XML messsages being passed 
> from a server to a client.  The parse method hangs until I close the 
> server output stream.
> 
> Anyway to signal to Digester.parse(InputStream) that it should return 
> without having reached the end of an inputstream?  Or do I have chose a 
> different parse method?

Digester just calls someXmlParser.parse(). If you can find an xml parser
that invokes endDocument then returns when it reaches the end of the
root element then Digester should work fine with that. I'm not aware of
any such parser though.

Maybe you could try writing some kind of filter for a standard xml
parser to make a parser act like that.

There might be a way to modify Digester itself to detect the end of the
root element (fairly simple), then to forcibly terminate the xml parser
(tricky). I'm not sure it's a stable solution, though, because there is
no rule against a SAX parser reading its input in chunks, then firing
off the relevant SAX events. Stopping the parser when the endElement
occurs for the root element therefore doesn't guarantee that no
"read-ahead" has occurred.

I would suggest using some other kind of string sequence in your input
stream to separate xml documents. You could then read a document into a
string buffer and run the parser on that. If the docs are large, you
could alternatively write a custom InputStream that indicates EOF when
it reaches your custom marker.

If you do come up with a clever solution for this, please let us know as
it's something that has been asked a few times.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org