in reply to Automatically generating XML

Try XML::AutoWriter. The random bit you'll have to do for yourself, but AutoWriter will stick to the DTD for you.