Advertisement
Python253

bin2xml

Mar 11th, 2024
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: bin2xml.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a binary file (.bin) to an XML file (.xml).
  10. It reads the binary content, assuming each line in the binary file represents a separate element.
  11. The script decodes each line from UTF-8 and creates an XML file with elements under the root.
  12.  
  13. Requirements:
  14. - Python 3.x
  15.  
  16. Usage:
  17. 1. Save this script as 'bin2xml.py'.
  18. 2. Ensure your binary file ('example.bin') is in the same directory as the script.
  19. 3. Run the script.
  20. 4. The converted XML file ('bin2xml.xml') will be generated in the same directory.
  21.  
  22. Note: Adjust the 'bin_filename' and 'xml_filename' variables in the script as needed.
  23. """
  24.  
  25. import xml.etree.ElementTree as ET
  26.  
  27. def bin_to_xml(bin_filename, xml_filename):
  28.  root = ET.Element("root")
  29.  with open(bin_filename, 'rb') as binfile:
  30.      for line in binfile:
  31.          # Assuming each line in the binary file is a separate element
  32.          ET.SubElement(root, "element").text = line.decode('utf-8').strip()
  33.  
  34.  tree = ET.ElementTree(root)
  35.  tree.write(xml_filename)
  36.  
  37. if __name__ == "__main__":
  38.  bin_filename = 'example.bin'
  39.  xml_filename = 'bin2xml.xml'
  40.  bin_to_xml(bin_filename, xml_filename)
  41.  print(f"Converted '{bin_filename}' to '{xml_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement