How to derive DTD (or other XML spec format) from XML file samples
http://www.stylusstudio.com/dtd_generator.html is actual software implementing a DTD generator.
http://www.pmg.csail.mit.edu/~chmoh/pubs/wecwis.pdf seems like a nice paper on the kind of thing you'd need, but I can't find (links to) actual code anywhere in the paper so far.
Here's another paper on this, again, no code to be found: http://www.softnet.tuc.gr/~minos/Papers/debull03.pdf.
Finally, I'd also suggest you look into using RELAX NG or Schematron to validate your XML instead. Those languages are much more expressive, making them easier to read and more powerful in the kinds of things you can validate. (Be sure to skip XML Schema, which is widely considered to be a mess.)
You can use the following link for generating schema online, by providing just the xml data.http://www.xmlforasp.net/codebank/system_xml_schema/buildschema/buildxmlschema.aspx