Xml in Python

Here’s the translation of the XML example from Go to Python:

Our program demonstrates how to work with XML in Python using the xml.etree.ElementTree module. Here’s the full source code:

import xml.etree.ElementTree as ET
from dataclasses import dataclass, field
from typing import List

@dataclass
class Plant:
    id: int
    name: str
    origin: List[str] = field(default_factory=list)

    def __str__(self):
        return f"Plant id={self.id}, name={self.name}, origin={self.origin}"

def main():
    coffee = Plant(id=27, name="Coffee")
    coffee.origin = ["Ethiopia", "Brazil"]

    # Create XML structure
    plant = ET.Element("plant")
    plant.set("id", str(coffee.id))
    name = ET.SubElement(plant, "name")
    name.text = coffee.name
    for origin in coffee.origin:
        origin_elem = ET.SubElement(plant, "origin")
        origin_elem.text = origin

    # Emit XML representing our plant
    xml_str = ET.tostring(plant, encoding="unicode", method="xml")
    pretty_xml = xml.dom.minidom.parseString(xml_str).toprettyxml(indent="  ")
    print(pretty_xml)

    # Add XML header
    print(f'<?xml version="1.0" encoding="UTF-8"?>\n{pretty_xml}')

    # Parse XML back into a Plant object
    parsed_plant = Plant(id=0, name="")
    root = ET.fromstring(xml_str)
    parsed_plant.id = int(root.get("id"))
    parsed_plant.name = root.find("name").text
    parsed_plant.origin = [origin.text for origin in root.findall("origin")]
    print(parsed_plant)

    # Create nested XML structure
    tomato = Plant(id=81, name="Tomato", origin=["Mexico", "California"])
    
    nesting = ET.Element("nesting")
    parent = ET.SubElement(nesting, "parent")
    child = ET.SubElement(parent, "child")
    
    for p in [coffee, tomato]:
        plant = ET.SubElement(child, "plant")
        plant.set("id", str(p.id))
        name = ET.SubElement(plant, "name")
        name.text = p.name
        for origin in p.origin:
            origin_elem = ET.SubElement(plant, "origin")
            origin_elem.text = origin

    nested_xml = ET.tostring(nesting, encoding="unicode", method="xml")
    pretty_nested_xml = xml.dom.minidom.parseString(nested_xml).toprettyxml(indent="  ")
    print(pretty_nested_xml)

if __name__ == "__main__":
    main()

This program demonstrates XML handling in Python:

  1. We define a Plant class using dataclass to represent our data structure.

  2. We create XML structures using the xml.etree.ElementTree module.

  3. We use ET.tostring() to convert the XML structure to a string, and xml.dom.minidom to pretty-print the XML.

  4. We demonstrate how to parse XML back into a Python object.

  5. We create a nested XML structure to show more complex XML creation.

To run the program, save it as xml_example.py and use python:

$ python xml_example.py

This will output the XML representations and the parsed Plant object.

Python’s xml.etree.ElementTree module provides a simple way to create, parse, and manipulate XML. For more complex XML operations, you might consider using libraries like lxml which offer additional features and better performance.