You will get that error if you have
anything before the XML prolog, which is the <?xml ... ?> part.
I don't quite get all that code you posted. None of it is the XML document, which is the first thing I would have looked at. But my guess is that you have posted something that generates XML, something that posts it to an HTTP URL, and something that runs on the server and tries to parse the posted XML. Have I disentangled the post correctly?
If I have, then clearly your XML document looks like
which, as I mentioned at the beginning, is not well-formed XML. If you want the XML you generate to include a DOCTYPE, then
you should just tell the Transformer to include one. Like this: