Xml + Sax
I'm trying to obtein an XML response from a server through a socket, and parsing it with SAX. On the server side the communication (i.e. the socket) should keep alive beyond each response.
My problem is that the parser keeps waiting for more data because the socket doesn't close after the response.
I've made something like a tricky solution by doing a response preview by myself and detecting the route tag. I create a separated StringReader where I append the text already read and once I recognize the closing route tag I stop reading from the socket and create a new InputSource for the parser.
I don`t like this solution, because I'm quite parsing the message twice and I must also keep a buffer for a possible piece of future message already read so as to include it in a future response.
What is the right solution here ?
Your parser could read the first tag that it gets. Then, it would keep reading until it encounters the matching tag. After the matching tag is encountered, it would stop trying to get more data, being at the end of the XML document.
1)add tag like <Size> which will represent a size of the message.
2)when you are reading response from stream you have to implement algorithm to make a read and analyze what you read checking whether you got full response or partial response and act accordingly.
3) you do not need to use parser for #2.
4) use your parser after you receive full message.