In addition to the fine tools mentioned above the translation suite "Transolution" also supports conversion of HTML/XML to XLIFF. It's hosting site have been down for some time but is now up again at this location: https://bitbucket.org/fredrik_corneliusson/transolution/overview
Transolution runs on both Windows and Linux.
You can find documentation about the filter here: https://bitbucket.org/fredrik_corneliusson/transolution/src/tip/doc/filters.rst?at=default
As the filter is a command line tool it would be quite easy to call it from code to programatically do the conversion.