BeautifulSoup gets you almost all the way there:

>>> import BeautifulSoup
>>> f = open('a.html')
>>> soup = BeautifulSoup.BeautifulSoup(f)
>>> f.close()
>>> g = open('a.xml', 'w')
>>> print >> g, soup.prettify()
>>> g.close()

This closes all tags properly. The only issue remaining is that the doctype remains HTML -- to change that into the doctype of your choice, you only need to change the first line, which is not hard, e.g., instead of printing the prettified text directly,

>>> lines = soup.prettify().splitlines()
>>> lines[0] = ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
                '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
>>> print >> g, '\n'.join(lines)
Answer from Alex Martelli on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › parsing-and-converting-html-documents-to-xml-format-using-python
Parsing and converting HTML documents to XML format using Python - GeeksforGeeks
July 23, 2025 - In this approach, we will use Python's lxml library to parse the HTML document and write it to an encoded string representation of the XML tree.The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt.
🌐
lxml
lxml.de › parsing.html
Parsing XML and HTML with lxml
Otherwise, you can resort at runtime to other (usually slower) Python tools for passing decompressed input into lxml or reading from the network. If you want to parse from a string (bytes or text) and still provide a base URL for the document (e.g. to support relative paths in an XInclude), you can pass the base_url keyword argument: >>> root = etree.fromstring(xml, base_url="http://where.it/is/from.xml")
🌐
Barbaresi
adrien.barbaresi.eu › blog › parsing-converting-lxml-html-tei.html
Parsing and converting HTML documents to XML format using Python’s lxml - Bits of Language: corpus linguistics, NLP and text analytics
December 22, 2015 - The more pythonic way would be to make sure all the necessary libraries are installed (something like apt-get install libxml2-dev libxslt1-dev python-dev), and then using a package manager such as pip: pip install lxml. Here are the modules required for basic manipulation: from __future__ import ...
🌐
Aspose
products.aspose.com › aspose.cells › python via java › conversion › html to xml
Convert HTML to XML in Python Excel Library
November 13, 2025 - Add a library reference (import the library) to your Python project. Load HTML file with an instance of Workbook. Convert HTML to XML by calling Workbook.save method.
🌐
Aspose
products.aspose.com › aspose.slides › python via .net › conversion › html to xml
Convert HTML to XML in Python | products.aspose.com
July 24, 2025 - Open the source HTML files in Python. Save result as XML file. You can also convert HTML and save to other file formats.
🌐
TutorialsPoint
tutorialspoint.com › parsing-and-converting-html-documents-to-xml-format-using-python
Parsing and Converting HTML Documents to XML format using Python
July 25, 2023 - After installation, we import the BeautifulSoup module and use it to convert the HTML text into a BeautifulSoup object. Then, using BeautifulSoup's methods and properties, we can traverse and extract data by accessing elements, attributes, or text. Another effective and robust Python package for processing HTML and XML documents is lxml.
Find elsewhere
🌐
Example Code
example-code.com › python › htmlToXml_convertFile.asp
CkPython Convert HTML File to XML File
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • Unicode C++ • VB.NET • VBScript • Visual Basic 6.0 • Visual FoxPro • Xojo Plugin
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding [1] is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated). method is either "xml", "html" or "text" (default is "xml").
🌐
Python Forum
python-forum.io › thread-21577.html
HTML to XML
October 5, 2019 - I'm trying to convert HTML to XML. Then load the XML into a database for data manipulation. I researched and came to the conclusion that I can use Python to do this. If I'm right I will start learning Python. Can anyone point me to the right directi...
🌐
Hurix Digital
hurix.com › blogs › how-to-convert-html-to-xml-efficiently
How to Convert HTML to XML Efficiently?
November 17, 2025 - Some popular options include Convertio, Online-Convert, and HTMLtoXML. These tools are easy to use and can handle large amounts of HTML code. They’re ideal for: ... If you have programming experience, you can write a script to convert HTML to XML.
🌐
Python.org
discuss.python.org › python help
Parsing HTML with the XML module - Python Help - Discussions on Python.org
November 16, 2021 - I need to parse HTML files with the Python 3.8 xml package. This must be possible because some of the xml.etree.ElementTree methods have parameters that take "xml" or "html" as a value, but I can’t find an example of how it’s done. I get an exception when I try to parse the HTML file: htmlRoot = etree.ElementTree.parse(filepathname).getroot() The parser throws an “undefined entity” exception when it encounters an HTML entity.
🌐
Testmuai
testmuai.com › home › free tools › html to xml converter online
HTML to XML Online Converter | TestMu AI
Need to convert HTML to XML? Our HTML to XML converter is fast, easy to use, and completely free. Convert your HTML tables to XML in just a few seconds.
🌐
Aspose
products.aspose.app › excel apps › conversion › html to xml
Convert HTML to XML Free Online - Aspose Products
Upload your HTML files to convert. Press the "CONVERT" button. Download the converted XML files instantly or send a download link to email.
🌐
O'Reilly
oreilly.com › library › view › python-xml › 0596001282 › ch03s06.html
Converting XML to HTML - Python & XML [Book]
December 18, 2001 - Converting XML to HTMLThe PyXML package contains XML parsers, including PyExpat, as well as support for SAX and DOM, and much more. While learning the ropes of the PyXML package, it... - Selection from Python & XML [Book]
Authors   Christopher A. JonesFred L. Drake
Published   2001
Pages   380
🌐
GitHub
github.com › Chadii › From-xml-to-html-python-script
GitHub - Chadii/From-xml-to-html-python-script: This is a small useful script that convert multiple output xml scan done with nmap with the command nmap ip -oX name.xml to html files. Use : Be sure to name the xml files as a number , for example : 1.xml , 25.xml ... and have the script in the same xml files folder , or change the path in the script .Video =>
This is a small useful script that convert multiple output xml scan done with nmap with the command nmap ip -oX name.xml to html files. Use : Be sure to name the xml files as a number , for example : 1.xml , 25.xml ... and have the script in ...
Starred by 5 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
Aspose
products.aspose.com › aspose.cells › python via .net › conversion › xml to html
Convert XML to HTML in Python Excel Library
November 13, 2025 - Need to convert XML files to HTML programmatically? Python developers can easily load & convert XML to HTML in just a few lines of code.
🌐
GitHub
github.com › topics › xml-to-html
xml-to-html · GitHub Topics · GitHub
json document-conversion xml convert apis xml-to-pdf json-to-pdf xml-to-html xml-to-png json-to-html json-to-png ... Abdal ReportGen is a fast, elegant, and extensible tool for transforming raw datasets into structured, styled, and modern HTML reports · python report html-report xml-to-html json-to-html report-generator excel-to-html csv-to-html ebrasha abdal ebrahim-shafiei sql-to-html