We usually turn off validation in production anyway. It is too resource intensive. So throughout development and
test it is on. This is to be sure that everyithing is good. Then turn it off and go to production.
This might be a solution for your situation where you do not have control over the xml.
Of course if you cannot trust that the originating XML builder is consistant and valid, you may need to try and find another solition that will work.
Most of the validation I have done is with XMLSchema, not DTD. I know that with XMLSchemas you can specify the schema at runtime. So, maybe this is what you are looking for. Here is an example of validating with xerces an XML file that does not have any namespaces in it: