I have a socket connection for an external system which accept commands and sends results in XML. Every command and result is a standalone XML document.
Which Java parser (/combination) should i use to:
- parse the stream continuously without closing the connection (i know it’s stupid, but i tried DOMParser in the past and it throws an exception when an another document root encountered on the stream which is perfectly understandable). I need something like: continously read the stream and when a document is fully received, do it’s processing. I don’t know how big the document is, so i need to leave to the parser to figure out the end of the document.
- deserialize every incoming document into bean instances (similary like XStream does)
- serialize command object to the output stream from annotated class instances (similarly like XStream does). I don’t want to use two separate libraries for sending and receiving.
Well… XStream.createObjectInputStream seems to be what you need. I’m not sure if the stream provided must enclose all objects into a root node, but anyway you could arrange an inputstreams that add some virtual content to accomodate to XStream needs. I’ll expand this answer later…
http://x-stream.github.io/objectstream.html has some samples…
Root node
Indeed the reader needs a root node. So you need an inputstream that reads
<object-stream>plus the real byte content, plus a</object-stream>at the end (if you mind about that end). Depending on what you need (inputstream, readers) the implementation can be slighly different but it can be done.Sample
You can use SequenceInputStream to concatenate virtual content to the original inputstream:
If you use readers… well. There must be something equivalent 🙂