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