{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "/employess.xml" ], "metadata": { "id": "Zw7fiPiytBf5" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import xml.etree.ElementTree as ET\n", "\n", "try:\n", " # Parse the XML file\n", " tree = ET.parse('/content/employess.xml')\n", " root = tree.getroot()\n", "\n", " # Print the root element\n", " print(\"Root Element:\", root.tag)\n", "\n", " # Iterate over all 'employee' elements\n", " for employee in root.findall('employee'):\n", " try:\n", " # Extract data from each 'employee'\n", " employee_id = employee.find('id').text\n", " name = employee.find('name').text\n", " role = employee.find('role').text\n", " age = employee.find('age').text\n", " department = employee.find('department').text\n", "\n", " # Print employee details\n", " print(f\"Employee ID: {employee_id}\")\n", " print(f\"Name: {name}\")\n", " print(f\"Role: {role}\")\n", " print(f\"Age: {age}\")\n", " print(f\"Department: {department}\")\n", " print(\"-\" * 20)\n", " except AttributeError as e:\n", " print(f\"Error processing employee data: {e}\")\n", " except Exception as e:\n", " print(f\"Unexpected error while processing employee: {e}\")\n", "\n", "except FileNotFoundError:\n", " print(\"Error: The XML file was not found. Please check the file path.\")\n", "except ET.ParseError as e:\n", " print(f\"Error: The XML file is malformed. Details: {e}\")\n", "except Exception as e:\n", " print(f\"An unexpected error occurred: {e}\")\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ef2MqAGZ20JA", "outputId": "24876c00-2550-484f-9071-af2d49fbfa64" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Root Element: company\n", "Employee ID: 1\n", "Name: John Doe\n", "Role: Software Developer\n", "Age: 29\n", "Department: Engineering\n", "--------------------\n", "Employee ID: 2\n", "Name: Jane Smith\n", "Role: Project Manager\n", "Age: 34\n", "Department: Management\n", "--------------------\n", "Employee ID: 3\n", "Name: Emily Davis\n", "Role: Data Scientist\n", "Age: 27\n", "Department: Data Science\n", "--------------------\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "GEnMDUaM3x5i" }, "execution_count": null, "outputs": [] } ] }