Title: Message
Hi David
 
Brian is right. In fact dom4j cannot determine by itself when the end of a document occurs since an XML document can contain other stuff after the end tag, e.g. comments or processing instructions.
 
<!-- this is a valid XML document -->
<something>
    hello
</something>
<!-- this is a comment -->
<?stylesheet file="foo.xslt"?>
 
As Brian says, using some kind of protocol is required to determine the length of the XML file - such as using a content length in the HTTP protocol. This simple mechanism can be implemented on top of sockets using streams.
 
If in doubt though, use HTTP (or Servlets) to do all this for you.

James
----- Original Message -----
Sent: Tuesday, March 26, 2002 9:59 PM
Subject: Re: [dom4j-user] Help reading xml from socket

David,

This is a common problem people encounter when working with sockets and probably not a dom4j issue.   The socket essentially stays open and there will not be a way to determine when the client is done sending data unless you investigate the data itself.  Flush() was a good try but it will not solve this problem.   I doubt  that dom4j is doing a lot of intelligence to find out when the whole document has been collected (i.e. looking for the end of the root tag, etc) You may want to consider implementing the HTTP protocol and sending the data off after you collect it.  HTTP has a content-length header variable so you know when to stop reading.  Or, you could look for the start and end tags occuring in the data yourself and then send the collected data on to dom4j.

Outside of your problem, for what it is worth- piping data right from a socket into dom4j is probably not the best design- I'd focus on collecting the data first like I mentioned above and then continuing.  This will give you an adequate abstraction layer for better error handling and so forth.  The current design is too tied to the network layer for my preference.  

Good luck!
Brian

David Hooker wrote:
000801c1d50c$e495c930$6901a8c0@DHOOKER type="cite">
I have a socket from which I'm receiving xml.  I need to create a Document object from this.  Once the xml comes in, I need to leave the socket open for other data to come in.  Here's the receiving code:
 
    private Document getDocumentFromStream(InputStream xmlStream)
    {
        Document doc = null;
 
        SAXReader reader = new SAXReader();
        reader.setValidation(false);
 
        try
        {
            doc = reader.read(xmlStream);
        }
        catch(DocumentException de)
        {
            System.out.println("error");
        }
        return doc;
    }
Now, the sender is calling flush() on the stream after it sends the xml, but it isn't closing the socket.  Does the SAXReader know when the end of the xml data is so that it can construct and return the Document - without me having to close the stream to signal it that there is no more data?

Reply via email to