extracted code from JavaXmlRequests. Fixes #2563