Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: xml2html.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts an XML file (.xml) to an HTML file (.html).
- It reads the text content of each element in the XML and writes it to the HTML file within <p> tags.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'xml2html.py'.
- 2. Ensure your XML file ('example.xml') is in the same directory as the script.
- 3. Run the script.
- 4. The converted HTML file ('xml2html.html') will be generated in the same directory.
- Note: Adjust the 'xml_filename' and 'html_filename' variables in the script as needed.
- """
- import xml.etree.ElementTree as ET
- def xml_to_html(xml_filename, html_filename):
- tree = ET.parse(xml_filename)
- root = tree.getroot()
- with open(html_filename, 'w') as htmlfile:
- htmlfile.write('<html><body>')
- for element in root.iter():
- if element.text:
- htmlfile.write('<p>' + element.text + '</p>')
- htmlfile.write('</body></html>')
- if __name__ == "__main__":
- # Set the filenames for the XML and HTML files
- xml_filename = 'example.xml'
- html_filename = 'xml2html.html'
- # Convert the XML to an HTML file
- xml_to_html(xml_filename, html_filename)
- print(f"Converted '{xml_filename}' to '{html_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement