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