How about this as a starting point if you have to write it:
- Convert the HTML to XML (e.g. with Tidy)
- Select the fields in which you are interested
- Use perl to process the xml and generate the code, probably with something based on XSLT
You might also want to check out CGI::XMLForm, which I have wanted to play with for some time.