Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: create_example_xml.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script creates an example XML file ('example.xml') with sample data.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'create_example_xml.py'.
- 2. Run the script.
- Note: Adjust the 'xml_filename' variable in the script as needed.
- """
- import xml.etree.ElementTree as ET
- def create_example_xml(filename):
- # Create the root element
- root = ET.Element("example_data")
- # Create child elements with data
- person1 = ET.SubElement(root, "person")
- ET.SubElement(person1, "name").text = "John Doe"
- ET.SubElement(person1, "age").text = "25"
- ET.SubElement(person1, "city").text = "New York"
- person2 = ET.SubElement(root, "person")
- ET.SubElement(person2, "name").text = "Jane Smith"
- ET.SubElement(person2, "age").text = "30"
- ET.SubElement(person2, "city").text = "San Francisco"
- person3 = ET.SubElement(root, "person")
- ET.SubElement(person3, "name").text = "Bob Johnson"
- ET.SubElement(person3, "age").text = "22"
- ET.SubElement(person3, "city").text = "Los Angeles"
- # Create the ElementTree and write to the XML file
- tree = ET.ElementTree(root)
- tree.write(filename)
- if __name__ == "__main__":
- # Set the filename for the example XML file
- xml_filename = 'example.xml'
- # Create the example XML file in the current working directory
- create_example_xml(xml_filename)
- print(f"Example XML file '{xml_filename}' created in the current working directory.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement