Python XML - Read/Write and Create

||
Posted 11 months ago
||
Views 163
||
4 min read
2 reactions

To read XML files recursively in Python, you can use the os module to traverse through directories and the xml.etree.ElementTree module to parse and process XML files. Here's an example program that reads XML files recursively:

import os
import xml.etree.ElementTree as ET

def process_xml_file(file_path):
    try:
        tree = ET.parse(file_path)
        root = tree.getroot()

        # Process XML data as needed
        # Example: Access elements and attributes
        for element in root.iter():
            print(f"Element: {element.tag}")
            print(f"Attributes: {element.attrib}")

    except ET.ParseError:
        print(f"Error parsing XML file: {file_path}")

def read_xml_files(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.xml'):
                file_path = os.path.join(root, file)
                process_xml_file(file_path)

# Specify the directory containing XML files
directory_path = 'path/to/xml/files'

# Call the function to read XML files recursively
read_xml_files(directory_path)

In this program, we define two functions. The process_xml_file function is responsible for parsing and processing an individual XML file. You can customize this function according to your specific requirements.

The read_xml_files function takes a directory path as an argument and uses os.walk to recursively traverse through the directory structure. It identifies XML files based on the file extension (in this example, '.xml') and calls the process_xml_file function for each XML file found.

To use this program, replace 'path/to/xml/files' with the actual path to the directory containing your XML files. When you run the program, it will recursively read all the XML files in the specified directory and print the element tag names and attributes. You can modify the process_xml_file function to perform the desired operations on the XML data.

import os
import xml.etree.ElementTree as ET

def process_xml_file(file_path):
    try:
        tree = ET.parse(file_path)
        root = tree.getroot()

        # Process XML data as needed
        # Example: Extract data from specific elements
        for person in root.iter('Person'):
            name = person.find('Name').text
            age = person.find('Age').text
            print(f"Name: {name}, Age: {age}")

    except ET.ParseError:
        print(f"Error parsing XML file: {file_path}")

def read_xml_files(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.xml'):
                file_path = os.path.join(root, file)
                process_xml_file(file_path)

# Specify the directory containing XML files
directory_path = 'path/to/xml/files'

# Call the function to read XML files recursively
read_xml_files(directory_path)

In this updated program, we have modified the process_xml_file function to extract data from specific elements within the XML file. In this example, we assume that each XML file contains <Person> elements, with nested <Name> and <Age> elements.

The program searches for <Person> elements using root.iter('Person') and then extracts the text content of the <Name> and <Age> elements using the find method. The extracted data is then printed for each <Person> element found in the XML file.

You can customize the code within the for loop in the process_xml_file function to extract and process data from other elements or perform different operations based on your XML structure and requirements.

Remember to replace 'path/to/xml/files' with the actual directory path containing your XML files. Running the program will recursively read all XML files in the specified directory and extract the desired data from them.

HOW TO CREATE

To create an XML file using Python, you can use the xml.etree.ElementTree module. This module provides a convenient and straightforward way to build an XML tree structure and write it to a file. Here's an example that demonstrates how to create an XML file:

import xml.etree.ElementTree as ET

# Create the root element
root = ET.Element("Employees")

# Create child elements
employee1 = ET.SubElement(root, "Employee")
employee1.set("id", "1")

name1 = ET.SubElement(employee1, "Name")
name1.text = "John Doe"

age1 = ET.SubElement(employee1, "Age")
age1.text = "30"

employee2 = ET.SubElement(root, "Employee")
employee2.set("id", "2")

name2 = ET.SubElement(employee2, "Name")
name2.text = "Jane Smith"

age2 = ET.SubElement(employee2, "Age")
age2.text = "35"

# Create the XML tree
tree = ET.ElementTree(root)

# Write the XML tree to a file
tree.write("employees.xml", encoding="utf-8", xml_declaration=True)

In this example, we start by creating the root element using ET.Element("Employees"). Then, we create child elements (Employee) and set attributes (id) using ET.SubElement and set. We set the text content of the child elements (Name and Age) using the text attribute.

Next, we create the XML tree using ET.ElementTree(root). Finally, we write the XML tree to a file using tree.write("employees.xml", encoding="utf-8", xml_declaration=True). You can specify the desired file name in the write function.

When you run this program, it will create an XML file named "employees.xml" in the same directory as your Python script. The file will contain the XML structure you built with the employee information.


2 reactions

Discussion


Looking for Freelancing Jobs
Joined on April 15, 2020

Latest Videos