Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: bin2xml.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a binary file (.bin) to an XML file (.xml).
- It reads the binary content, assuming each line in the binary file represents a separate element.
- The script decodes each line from UTF-8 and creates an XML file with elements under the root.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'bin2xml.py'.
- 2. Ensure your binary file ('example.bin') is in the same directory as the script.
- 3. Run the script.
- 4. The converted XML file ('bin2xml.xml') will be generated in the same directory.
- Note: Adjust the 'bin_filename' and 'xml_filename' variables in the script as needed.
- """
- import xml.etree.ElementTree as ET
- def bin_to_xml(bin_filename, xml_filename):
- root = ET.Element("root")
- with open(bin_filename, 'rb') as binfile:
- for line in binfile:
- # Assuming each line in the binary file is a separate element
- ET.SubElement(root, "element").text = line.decode('utf-8').strip()
- tree = ET.ElementTree(root)
- tree.write(xml_filename)
- if __name__ == "__main__":
- bin_filename = 'example.bin'
- xml_filename = 'bin2xml.xml'
- bin_to_xml(bin_filename, xml_filename)
- print(f"Converted '{bin_filename}' to '{xml_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement