Nothing I propose here is tested, but these are the routes I think I would take.
If the response length is expected to be small, I would personally probably just go for placing the concatenated XML response into a String
as you suggest, and then either use standard String
methods to extract the individual XML documents, or again as you suggested, remove the XML declaration strings and wrap the whole lot with a pair of root elements. It would depend on whether you wanted to feed your XML parser with a single document or multiple. I haven't dealt with BasicHttpResponse
in ages, but I think you can get an InputStream
of the response entity using mBasicHttpResponse.getEntity().getContent()
, and then use one of many ways possible to obtain a String
from that InputStream
.
If on the other hand I expect to be dealing with pretty lengthy data or if the response entity could contain an indeterminate number of concatenated XML documents, I would then instead think about wrapping the obtained InputStream
with a custom InputStream
or Reader
that performs (a) stripping away of the declarations and (b) insertion of new root elements. There's someone else on SO who asked a very similar question to the problem you're facing here except he didn't have the declarations to deal with. Looking at user656449's answer, we see a suggestion of how to wrap an InputStream
with some dummy root elements before passing it to the SAX parser:
(Blatantly copied from referenced SO question / answer):
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser parser = saxFactory.newSAXParser();
parser.parse(
new SequenceInputStream(
Collections.enumeration(Arrays.asList(
new InputStream[] {
new ByteArrayInputStream("<dummy>".getBytes()),
new FileInputStream(file),//bogus xml
new ByteArrayInputStream("</dummy>".getBytes()),
}))
),
new DefaultHandler()
);
But additionally in this situation, you would replace the FileInputStream
with some kind of CustomFilterFileInputStream
that you create yourself to perform stripping of the declaration lines. Your CustomFilterFileInputStream
would wrap around the InputStream
obtained from your BasicHttpResponse
and then use of the SequenceInputStream
adds the new root tags.
That's the kind of direction I think you would need to go if you really have to accept the XML data in this way, and if you expect to deal with large amounts of it in a single response.