DocumentBuilder.parse(InputSource is)

D

David

Hello guys,

I have an InputSource from a socket where XML data is poured. Id like to
interprete each XML data message separatly. But the
DocumentBuilder.parse(InputSource is) won't parse until the socket is
closed (hence when the InputSource is closed)

Anyone can give me some direction to solve this problem please?
 
F

Filip Larsen

David wrote
I have an InputSource from a socket where XML data is poured. Id like to
interprete each XML data message separatly. But the
DocumentBuilder.parse(InputSource is) won't parse until the socket is
closed (hence when the InputSource is closed)

If you have full control over both client and server the easiest is
perhaps to use your own protocol to separate each XML message from each
other, for instance by sending how many characters (or bytes) the next
message is. You then just read in that many characters fully into a
buffer and parse that as a complete XML document. ByteArrayOutputStream
and ByteArrayInputStream are your friends here. Alternatively you can
scan for the xml header string and end tag (easy if it is uniqely named,
complicated if it is not) and then extract the section of data including
both into a buffer for parsing.

If your messages are sufficiently simple in structure and generated on
the fly, it may be even easier and less resource using to let the
complete stream be one valid XML stream where each message is a child of
the top level tag. The complete stream can then be parsed using SAX, as
it can handle never-ending-streams just fine. This is good for long
simple messages, but not so good if your messages have a complicated
structure, or if the rest of the application need the messages as a DOM
tree anyway.


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top