Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: json_to_xml.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a JSON file (.json) to an XML file (.xml).
- It reads the JSON file and creates an XML file with the JSON content.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'json_to_xml.py'.
- 2. Ensure your JSON file ('example.json') is in the same directory as the script.
- 3. Run the script.
- Note: Adjust the 'json_filename' and 'xml_filename' variables in the script as needed.
- """
- import json
- import xml.etree.ElementTree as ET
- def json_to_xml(json_filename, xml_filename):
- with open(json_filename, 'r') as jsonfile:
- data = json.load(jsonfile)
- root = ET.Element("root")
- if "text_content" in data:
- ET.SubElement(root, "element").text = data["text_content"]
- tree = ET.ElementTree(root)
- tree.write(xml_filename)
- if __name__ == "__main__":
- json_filename = 'example.json'
- xml_filename = 'json2xml.xml'
- json_to_xml(json_filename, xml_filename)
- print(f"Converted '{json_filename}' to '{xml_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement