File size: 3,571 Bytes
0e56c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{
  "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": []
    }
  ]
}