ronakreddy18 commited on
Commit
0e56c9d
·
verified ·
1 Parent(s): df988c2

Upload XML_FILE_HANDLING.ipynb

Browse files
Files changed (1) hide show
  1. pages/XML_FILE_HANDLING.ipynb +116 -0
pages/XML_FILE_HANDLING.ipynb ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "/employess.xml"
21
+ ],
22
+ "metadata": {
23
+ "id": "Zw7fiPiytBf5"
24
+ },
25
+ "execution_count": null,
26
+ "outputs": []
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "source": [
31
+ "import xml.etree.ElementTree as ET\n",
32
+ "\n",
33
+ "try:\n",
34
+ " # Parse the XML file\n",
35
+ " tree = ET.parse('/content/employess.xml')\n",
36
+ " root = tree.getroot()\n",
37
+ "\n",
38
+ " # Print the root element\n",
39
+ " print(\"Root Element:\", root.tag)\n",
40
+ "\n",
41
+ " # Iterate over all 'employee' elements\n",
42
+ " for employee in root.findall('employee'):\n",
43
+ " try:\n",
44
+ " # Extract data from each 'employee'\n",
45
+ " employee_id = employee.find('id').text\n",
46
+ " name = employee.find('name').text\n",
47
+ " role = employee.find('role').text\n",
48
+ " age = employee.find('age').text\n",
49
+ " department = employee.find('department').text\n",
50
+ "\n",
51
+ " # Print employee details\n",
52
+ " print(f\"Employee ID: {employee_id}\")\n",
53
+ " print(f\"Name: {name}\")\n",
54
+ " print(f\"Role: {role}\")\n",
55
+ " print(f\"Age: {age}\")\n",
56
+ " print(f\"Department: {department}\")\n",
57
+ " print(\"-\" * 20)\n",
58
+ " except AttributeError as e:\n",
59
+ " print(f\"Error processing employee data: {e}\")\n",
60
+ " except Exception as e:\n",
61
+ " print(f\"Unexpected error while processing employee: {e}\")\n",
62
+ "\n",
63
+ "except FileNotFoundError:\n",
64
+ " print(\"Error: The XML file was not found. Please check the file path.\")\n",
65
+ "except ET.ParseError as e:\n",
66
+ " print(f\"Error: The XML file is malformed. Details: {e}\")\n",
67
+ "except Exception as e:\n",
68
+ " print(f\"An unexpected error occurred: {e}\")\n"
69
+ ],
70
+ "metadata": {
71
+ "colab": {
72
+ "base_uri": "https://localhost:8080/"
73
+ },
74
+ "id": "ef2MqAGZ20JA",
75
+ "outputId": "24876c00-2550-484f-9071-af2d49fbfa64"
76
+ },
77
+ "execution_count": 5,
78
+ "outputs": [
79
+ {
80
+ "output_type": "stream",
81
+ "name": "stdout",
82
+ "text": [
83
+ "Root Element: company\n",
84
+ "Employee ID: 1\n",
85
+ "Name: John Doe\n",
86
+ "Role: Software Developer\n",
87
+ "Age: 29\n",
88
+ "Department: Engineering\n",
89
+ "--------------------\n",
90
+ "Employee ID: 2\n",
91
+ "Name: Jane Smith\n",
92
+ "Role: Project Manager\n",
93
+ "Age: 34\n",
94
+ "Department: Management\n",
95
+ "--------------------\n",
96
+ "Employee ID: 3\n",
97
+ "Name: Emily Davis\n",
98
+ "Role: Data Scientist\n",
99
+ "Age: 27\n",
100
+ "Department: Data Science\n",
101
+ "--------------------\n"
102
+ ]
103
+ }
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "source": [],
109
+ "metadata": {
110
+ "id": "GEnMDUaM3x5i"
111
+ },
112
+ "execution_count": null,
113
+ "outputs": []
114
+ }
115
+ ]
116
+ }