Export data to an XML file. :param data: List of dictionaries containing the data to export :param filename: Name of the file to save the XML data :param root_element: Name of the root element in the XML structure
(
data: List[Dict[str, Any]], filename: str, root_element: str = "data"
)
| 45 | |
| 46 | |
| 47 | def export_to_xml( |
| 48 | data: List[Dict[str, Any]], filename: str, root_element: str = "data" |
| 49 | ) -> None: |
| 50 | """ |
| 51 | Export data to an XML file. |
| 52 | |
| 53 | :param data: List of dictionaries containing the data to export |
| 54 | :param filename: Name of the file to save the XML data |
| 55 | :param root_element: Name of the root element in the XML structure |
| 56 | """ |
| 57 | root = ET.Element(root_element) |
| 58 | for item in data: |
| 59 | element = ET.SubElement(root, "item") |
| 60 | for key, value in item.items(): |
| 61 | sub_element = ET.SubElement(element, key) |
| 62 | sub_element.text = str(value) |
| 63 | |
| 64 | tree = ET.ElementTree(root) |
| 65 | tree.write(filename, encoding="utf-8", xml_declaration=True) |
| 66 | logger.info("Data exported to %s", filename) |