-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmanipulating-xml.html
More file actions
144 lines (118 loc) · 44.5 KB
/
manipulating-xml.html
File metadata and controls
144 lines (118 loc) · 44.5 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Python - Manipulating XML</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Opening and reading using an ElementTree, Modifying an XML File, Create and Build XML Documents, Opening and reading large XML files using iterparse (incremental parsing), Searching the XML with XPath">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="Python - Manipulating XML">
<meta property="og:description" content="Opening and reading using an ElementTree, Modifying an XML File, Create and Build XML Documents, Opening and reading large XML files using iterparse (incremental parsing), Searching the XML with XPath">
<meta property="og:type" content="article">
<meta property="og:url" content="/python/manipulating-xml.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="Python - Manipulating XML">
<meta name="twitter:description" content="Opening and reading using an ElementTree, Modifying an XML File, Create and Build XML Documents, Opening and reading large XML files using iterparse (incremental parsing), Searching the XML with XPath">
<meta name="twitter:url" content="/python/manipulating-xml.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/2742.d9f697d0.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Python</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/python/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/python/getting-started-with-python-language.html" class="sidebar-link">Getting started with Python Language</a></li><li><a href="/python/python-data-types.html" class="sidebar-link">Python Data Types</a></li><li><a href="/python/indentation.html" class="sidebar-link">Indentation</a></li><li><a href="/python/comments-and-documentation.html" class="sidebar-link">Comments and Documentation</a></li><li><a href="/python/date-and-time.html" class="sidebar-link">Date and Time</a></li><li><a href="/python/enum.html" class="sidebar-link">Enum</a></li><li><a href="/python/set.html" class="sidebar-link">Set</a></li><li><a href="/python/simple-mathematical-operators.html" class="sidebar-link">Simple Mathematical Operators</a></li><li><a href="/python/bitwise-operators.html" class="sidebar-link">Bitwise Operators</a></li><li><a href="/python/boolean-operators.html" class="sidebar-link">Boolean Operators</a></li><li><a href="/python/operator-precedence.html" class="sidebar-link">Operator Precedence</a></li><li><a href="/python/variable-scope-and-binding.html" class="sidebar-link">Variable Scope and Binding</a></li><li><a href="/python/conditionals.html" class="sidebar-link">Conditionals</a></li><li><a href="/python/comparisons.html" class="sidebar-link">Comparisons</a></li><li><a href="/python/loops.html" class="sidebar-link">Loops</a></li><li><a href="/python/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/python/multidimensional-arrays.html" class="sidebar-link">Multidimensional arrays</a></li><li><a href="/python/dictionary.html" class="sidebar-link">Dictionary</a></li><li><a href="/python/list.html" class="sidebar-link">List</a></li><li><a href="/python/list-comprehensions.html" class="sidebar-link">List Comprehensions</a></li><li><a href="/python/list-slicing-selecting-parts-of-lists.html" class="sidebar-link">List slicing (selecting parts of lists)</a></li><li><a href="/python/groupby.html" class="sidebar-link">groupby()</a></li><li><a href="/python/linked-lists.html" class="sidebar-link">Linked lists</a></li><li><a href="/python/linked-list-node.html" class="sidebar-link">Linked List Node</a></li><li><a href="/python/filter.html" class="sidebar-link">Filter</a></li><li><a href="/python/heapq.html" class="sidebar-link">Heapq</a></li><li><a href="/python/tuple.html" class="sidebar-link">Tuple</a></li><li><a href="/python/basic-input-and-output.html" class="sidebar-link">Basic Input and Output</a></li><li><a href="/python/files-folders-i-o.html" class="sidebar-link">Files & Folders I/O</a></li><li><a href="/python/os-path.html" class="sidebar-link">os.path</a></li><li><a href="/python/iterables-and-iterators.html" class="sidebar-link">Iterables and Iterators</a></li><li><a href="/python/functions.html" class="sidebar-link">Functions</a></li><li><a href="/python/defining-functions-with-list-arguments.html" class="sidebar-link">Defining functions with list arguments</a></li><li><a href="/python/functional-programming-in-python.html" class="sidebar-link">Functional Programming in Python</a></li><li><a href="/python/partial-functions.html" class="sidebar-link">Partial functions</a></li><li><a href="/python/decorators.html" class="sidebar-link">Decorators</a></li><li><a href="/python/classes.html" class="sidebar-link">Classes</a></li><li><a href="/python/metaclasses.html" class="sidebar-link">Metaclasses</a></li><li><a href="/python/string-formatting.html" class="sidebar-link">String Formatting</a></li><li><a href="/python/string-methods.html" class="sidebar-link">String Methods</a></li><li><a href="/python/using-loops-within-functions.html" class="sidebar-link">Using loops within functions</a></li><li><a href="/python/importing-modules.html" class="sidebar-link">Importing modules</a></li><li><a href="/python/difference-between-module-and-package.html" class="sidebar-link">Difference between Module and Package</a></li><li><a href="/python/math-module.html" class="sidebar-link">Math Module</a></li><li><a href="/python/complex-math.html" class="sidebar-link">Complex math</a></li><li><a href="/python/collections-module.html" class="sidebar-link">Collections module</a></li><li><a href="/python/operator-module.html" class="sidebar-link">Operator module</a></li><li><a href="/python/json-module.html" class="sidebar-link">JSON Module</a></li><li><a href="/python/sqlite3-module.html" class="sidebar-link">Sqlite3 Module</a></li><li><a href="/python/the-os-module.html" class="sidebar-link">The os Module</a></li><li><a href="/python/the-locale-module.html" class="sidebar-link">The locale Module</a></li><li><a href="/python/itertools-module.html" class="sidebar-link">Itertools Module</a></li><li><a href="/python/asyncio-module.html" class="sidebar-link">Asyncio Module</a></li><li><a href="/python/random-module.html" class="sidebar-link">Random module</a></li><li><a href="/python/functools-module.html" class="sidebar-link">Functools Module</a></li><li><a href="/python/the-dis-module.html" class="sidebar-link">The dis module</a></li><li><a href="/python/the-base64-module.html" class="sidebar-link">The base64 Module</a></li><li><a href="/python/queue-module.html" class="sidebar-link">Queue Module</a></li><li><a href="/python/deque-module.html" class="sidebar-link">Deque Module</a></li><li><a href="/python/webbrowser-module.html" class="sidebar-link">Webbrowser Module</a></li><li><a href="/python/tkinter.html" class="sidebar-link">tkinter</a></li><li><a href="/python/pyautogui-module.html" class="sidebar-link">pyautogui module</a></li><li><a href="/python/indexing-and-slicing.html" class="sidebar-link">Indexing and Slicing</a></li><li><a href="/python/plotting-with-matplotlib.html" class="sidebar-link">Plotting with Matplotlib</a></li><li><a href="/python/graph-tool.html" class="sidebar-link">graph-tool</a></li><li><a href="/python/generators.html" class="sidebar-link">Generators</a></li><li><a href="/python/reduce.html" class="sidebar-link">Reduce</a></li><li><a href="/python/map-function.html" class="sidebar-link">Map Function</a></li><li><a href="/python/exponentiation.html" class="sidebar-link">Exponentiation</a></li><li><a href="/python/searching.html" class="sidebar-link">Searching</a></li><li><a href="/python/sorting-minimum-and-maximum.html" class="sidebar-link">Sorting, Minimum and Maximum</a></li><li><a href="/python/counting.html" class="sidebar-link">Counting</a></li><li><a href="/python/the-print-function.html" class="sidebar-link">The Print Function</a></li><li><a href="/python/regular-expressions-regex.html" class="sidebar-link">Regular Expressions (Regex)</a></li><li><a href="/python/copying-data.html" class="sidebar-link">Copying data</a></li><li><a href="/python/context-managers-with-statement.html" class="sidebar-link">Context Managers (“with” Statement)</a></li><li><a href="/python/the-name-special-variable.html" class="sidebar-link">The _name_ special variable</a></li><li><a href="/python/checking-path-existence-and-permissions.html" class="sidebar-link">Checking Path Existence and Permissions</a></li><li><a href="/python/creating-python-packages.html" class="sidebar-link">Creating Python packages</a></li><li><a href="/python/usage-of-pip-module-pypi-package-manager.html" class="sidebar-link">Usage of "pip" module: PyPI Package Manager</a></li><li><a href="/python/pip-pypi-package-manager.html" class="sidebar-link">pip: PyPI Package Manager</a></li><li><a href="/python/parsing-command-line-arguments.html" class="sidebar-link">Parsing Command Line arguments</a></li><li><a href="/python/subprocess-library.html" class="sidebar-link">Subprocess Library</a></li><li><a href="/python/setup-py.html" class="sidebar-link">setup.py</a></li><li><a href="/python/recursion.html" class="sidebar-link">Recursion</a></li><li><a href="/python/type-hints.html" class="sidebar-link">Type Hints</a></li><li><a href="/python/exceptions.html" class="sidebar-link">Exceptions</a></li><li><a href="/python/raise-custom-errors-exceptions.html" class="sidebar-link">Raise Custom Errors / Exceptions</a></li><li><a href="/python/commonwealth-exceptions.html" class="sidebar-link">Commonwealth Exceptions</a></li><li><a href="/python/urllib.html" class="sidebar-link">urllib</a></li><li><a href="/python/web-scraping-with-python.html" class="sidebar-link">Web scraping with Python</a></li><li><a href="/python/html-parsing.html" class="sidebar-link">HTML Parsing</a></li><li><a href="/python/manipulating-xml.html" aria-current="page" class="active sidebar-link">Manipulating XML</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/python/manipulating-xml.html#opening-and-reading-using-an-elementtree" class="sidebar-link">Opening and reading using an ElementTree</a></li><li class="sidebar-sub-header"><a href="/python/manipulating-xml.html#modifying-an-xml-file" class="sidebar-link">Modifying an XML File</a></li><li class="sidebar-sub-header"><a href="/python/manipulating-xml.html#create-and-build-xml-documents" class="sidebar-link">Create and Build XML Documents</a></li><li class="sidebar-sub-header"><a href="/python/manipulating-xml.html#opening-and-reading-large-xml-files-using-iterparse-incremental-parsing" class="sidebar-link">Opening and reading large XML files using iterparse (incremental parsing)</a></li><li class="sidebar-sub-header"><a href="/python/manipulating-xml.html#searching-the-xml-with-xpath" class="sidebar-link">Searching the XML with XPath</a></li></ul></li><li><a href="/python/python-requests-post.html" class="sidebar-link">Python Requests Post</a></li><li><a href="/python/distribution.html" class="sidebar-link">Distribution</a></li><li><a href="/python/property-objects.html" class="sidebar-link">Property Objects</a></li><li><a href="/python/overloading.html" class="sidebar-link">Overloading</a></li><li><a href="/python/polymorphism.html" class="sidebar-link">Polymorphism</a></li><li><a href="/python/method-overriding.html" class="sidebar-link">Method Overriding</a></li><li><a href="/python/user-defined-methods.html" class="sidebar-link">User-Defined Methods</a></li><li><a href="/python/string-representations-of-class-instances-str-and-repr-methods.html" class="sidebar-link">String representations of class instances: _str and repr_ methods</a></li><li><a href="/python/debugging.html" class="sidebar-link">Debugging</a></li><li><a href="/python/reading-and-writing-csv.html" class="sidebar-link">Reading and Writing CSV</a></li><li><a href="/python/writing-to-csv-from-string-or-list.html" class="sidebar-link">Writing to CSV from String or List</a></li><li><a href="/python/dynamic-code-execution-with-exec-and-eval.html" class="sidebar-link">Dynamic code execution with exec and eval</a></li><li><a href="/python/pyinstaller-distributing-python-code.html" class="sidebar-link">PyInstaller - Distributing Python Code</a></li><li><a href="/python/data-visualization-with-python.html" class="sidebar-link">Data Visualization with Python</a></li><li><a href="/python/the-interpreter-command-line-console.html" class="sidebar-link">The Interpreter (Command Line Console)</a></li><li><a href="/python/args-and-kwargs.html" class="sidebar-link">args and *kwargs</a></li><li><a href="/python/garbage-collection.html" class="sidebar-link">Garbage Collection</a></li><li><a href="/python/pickle-data-serialisation.html" class="sidebar-link">Pickle data serialisation</a></li><li><a href="/python/binary-data.html" class="sidebar-link">Binary Data</a></li><li><a href="/python/idioms.html" class="sidebar-link">Idioms</a></li><li><a href="/python/data-serialization.html" class="sidebar-link">Data Serialization</a></li><li><a href="/python/multiprocessing.html" class="sidebar-link">Multiprocessing</a></li><li><a href="/python/multithreading.html" class="sidebar-link">Multithreading</a></li><li><a href="/python/processes-and-threads.html" class="sidebar-link">Processes and Threads</a></li><li><a href="/python/python-concurrency.html" class="sidebar-link">Python concurrency</a></li><li><a href="/python/parallel-computation.html" class="sidebar-link">Parallel computation</a></li><li><a href="/python/sockets.html" class="sidebar-link">Sockets</a></li><li><a href="/python/websockets.html" class="sidebar-link">Websockets</a></li><li><a href="/python/sockets-and-message-encryption-decryption-between-client-and-server.html" class="sidebar-link">Sockets And Message Encryption/Decryption Between Client and Server</a></li><li><a href="/python/python-networking.html" class="sidebar-link">Python Networking</a></li><li><a href="/python/python-http-server.html" class="sidebar-link">Python HTTP Server</a></li><li><a href="/python/flask.html" class="sidebar-link">Flask</a></li><li><a href="/python/introduction-to-rabbitmq-using-amqpstorm.html" class="sidebar-link">Introduction to RabbitMQ using AMQPStorm</a></li><li><a href="/python/descriptor.html" class="sidebar-link">Descriptor</a></li><li><a href="/python/tempfile-namedtemporaryfile.html" class="sidebar-link">tempfile NamedTemporaryFile</a></li><li><a href="/python/input-subset-and-output-external-data-files-using-pandas.html" class="sidebar-link">Input, Subset and Output External Data Files using Pandas</a></li><li><a href="/python/unzipping-files.html" class="sidebar-link">Unzipping Files</a></li><li><a href="/python/working-with-zip-archives.html" class="sidebar-link">Working with ZIP archives</a></li><li><a href="/python/getting-start-with-gzip.html" class="sidebar-link">getting start with GZip</a></li><li><a href="/python/stack.html" class="sidebar-link">Stack</a></li><li><a href="/python/working-around-the-global-interpreter-lock-gil.html" class="sidebar-link">Working around the Global Interpreter Lock (GIL)</a></li><li><a href="/python/deployment.html" class="sidebar-link">Deployment</a></li><li><a href="/python/logging.html" class="sidebar-link">Logging</a></li><li><a href="/python/web-server-gateway-interface-wsgi.html" class="sidebar-link">Web Server Gateway Interface (WSGI)</a></li><li><a href="/python/python-server-sent-events.html" class="sidebar-link">Python Server Sent Events</a></li><li><a href="/python/alternatives-to-switch-statement-from-other-languages.html" class="sidebar-link">Alternatives to switch statement from other languages</a></li><li><a href="/python/list-destructuring-aka-packing-and-unpacking.html" class="sidebar-link">List destructuring (aka packing and unpacking)</a></li><li><a href="/python/accessing-python-source-code-and-bytecode.html" class="sidebar-link">Accessing Python source code and bytecode</a></li><li><a href="/python/mixins.html" class="sidebar-link">Mixins</a></li><li><a href="/python/attribute-access.html" class="sidebar-link">Attribute Access</a></li><li><a href="/python/arcpy.html" class="sidebar-link">ArcPy</a></li><li><a href="/python/abstract-base-classes-abc.html" class="sidebar-link">Abstract Base Classes (abc)</a></li><li><a href="/python/plugin-and-extension-classes.html" class="sidebar-link">Plugin and Extension Classes</a></li><li><a href="/python/immutable-datatypes-int-float-str-tuple-and-frozensets.html" class="sidebar-link">Immutable datatypes(int, float, str, tuple and frozensets)</a></li><li><a href="/python/incompatibilities-moving-from-python-2-to-python-3.html" class="sidebar-link">Incompatibilities moving from Python 2 to Python 3</a></li><li><a href="/python/2to3-tool.html" class="sidebar-link">2to3 tool</a></li><li><a href="/python/non-official-python-implementations.html" class="sidebar-link">Non-official Python implementations</a></li><li><a href="/python/abstract-syntax-tree.html" class="sidebar-link">Abstract syntax tree</a></li><li><a href="/python/unicode-and-bytes.html" class="sidebar-link">Unicode and bytes</a></li><li><a href="/python/python-serial-communication-pyserial.html" class="sidebar-link">Python Serial Communication (pyserial)</a></li><li><a href="/python/neo4j-and-cypher-using-py2neo.html" class="sidebar-link">Neo4j and Cypher using Py2Neo</a></li><li><a href="/python/basic-curses-with-python.html" class="sidebar-link">Basic Curses with Python</a></li><li><a href="/python/templates-in-python.html" class="sidebar-link">Templates in python</a></li><li><a href="/python/pillow.html" class="sidebar-link">Pillow</a></li><li><a href="/python/the-pass-statement.html" class="sidebar-link">The pass statement</a></li><li><a href="/python/cli-subcommands-with-precise-help-output.html" class="sidebar-link">CLI subcommands with precise help output</a></li><li><a href="/python/database-access.html" class="sidebar-link">Database Access</a></li><li><a href="/python/connecting-python-to-sql-server.html" class="sidebar-link">Connecting Python to SQL Server</a></li><li><a href="/python/postgresql.html" class="sidebar-link">PostgreSQL</a></li><li><a href="/python/python-and-excel.html" class="sidebar-link">Python and Excel</a></li><li><a href="/python/turtle-graphics.html" class="sidebar-link">Turtle Graphics</a></li><li><a href="/python/python-persistence.html" class="sidebar-link">Python Persistence</a></li><li><a href="/python/design-patterns.html" class="sidebar-link">Design Patterns</a></li><li><a href="/python/hashlib.html" class="sidebar-link">hashlib</a></li><li><a href="/python/creating-a-windows-service-using-python.html" class="sidebar-link">Creating a Windows service using Python</a></li><li><a href="/python/mutable-vs-immutable-and-hashable-in-python.html" class="sidebar-link">Mutable vs Immutable (and Hashable) in Python</a></li><li><a href="/python/configparser.html" class="sidebar-link">configparser</a></li><li><a href="/python/optical-character-recognition.html" class="sidebar-link">Optical Character Recognition</a></li><li><a href="/python/virtual-environments.html" class="sidebar-link">Virtual environments</a></li><li><a href="/python/python-virtual-environment-virtualenv.html" class="sidebar-link">Python Virtual Environment - virtualenv</a></li><li><a href="/python/virtual-environment-with-virtualenvwrapper.html" class="sidebar-link">virtual environment with virtualenvwrapper</a></li><li><a href="/python/create-virtual-environment-with-virtualenvwrapper-in-windows.html" class="sidebar-link">Create virtual environment with virtualenvwrapper in windows</a></li><li><a href="/python/sys.html" class="sidebar-link">sys</a></li><li><a href="/python/chempy-python-package.html" class="sidebar-link">ChemPy - python package</a></li><li><a href="/python/pygame.html" class="sidebar-link">pygame</a></li><li><a href="/python/pyglet.html" class="sidebar-link">Pyglet</a></li><li><a href="/python/audio.html" class="sidebar-link">Audio</a></li><li><a href="/python/pyaudio.html" class="sidebar-link">pyaudio</a></li><li><a href="/python/shelve.html" class="sidebar-link">shelve</a></li><li><a href="/python/iot-programming-with-python-and-raspberry-pi.html" class="sidebar-link">IoT Programming with Python and Raspberry PI</a></li><li><a href="/python/kivy-cross-platform-python-framework-for-nui-development.html" class="sidebar-link">kivy - Cross-platform Python Framework for NUI Development</a></li><li><a href="/python/pandas-transform-preform-operations-on-groups-and-concatenate-the-results.html" class="sidebar-link">Pandas Transform: Preform operations on groups and concatenate the results</a></li><li><a href="/python/similarities-in-syntax-differences-in-meaning-python-vs-javascript.html" class="sidebar-link">Similarities in syntax, Differences in meaning: Python vs. JavaScript</a></li><li><a href="/python/call-python-from-c.html" class="sidebar-link">Call Python from C#</a></li><li><a href="/python/ctypes.html" class="sidebar-link">ctypes</a></li><li><a href="/python/writing-extensions.html" class="sidebar-link">Writing extensions</a></li><li><a href="/python/python-lex-yacc.html" class="sidebar-link">Python Lex-Yacc</a></li><li><a href="/python/unit-testing.html" class="sidebar-link">Unit Testing</a></li><li><a href="/python/py-test.html" class="sidebar-link">py.test</a></li><li><a href="/python/profiling.html" class="sidebar-link">Profiling</a></li><li><a href="/python/python-speed-of-program.html" class="sidebar-link">Python speed of program</a></li><li><a href="/python/performance-optimization.html" class="sidebar-link">Performance optimization</a></li><li><a href="/python/security-and-cryptography.html" class="sidebar-link">Security and Cryptography</a></li><li><a href="/python/secure-shell-connection-in-python.html" class="sidebar-link">Secure Shell Connection in Python</a></li><li><a href="/python/python-anti-patterns.html" class="sidebar-link">Python Anti-Patterns</a></li><li><a href="/python/common-pitfalls.html" class="sidebar-link">Common Pitfalls</a></li><li><a href="/python/hidden-features.html" class="sidebar-link">Hidden Features</a></li><li><a href="/python/date-formatting.html" class="sidebar-link">Date Formatting</a></li><li><a href="/python/ijson.html" class="sidebar-link">ijson</a></li><li><a href="/python/django.html" class="sidebar-link">Django</a></li><li><a href="/python/code-blocks-execution-frames-and-namespaces.html" class="sidebar-link">Code blocks, execution frames, and namespaces</a></li><li><a href="/python/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="manipulating-xml"><a href="#manipulating-xml" class="header-anchor">#</a> Manipulating XML</h1> <h2 id="opening-and-reading-using-an-elementtree"><a href="#opening-and-reading-using-an-elementtree" class="header-anchor">#</a> Opening and reading using an ElementTree</h2> <p>Import the ElementTree object, open the relevant .xml file and get the root tag:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> xml<span class="token punctuation">.</span>etree<span class="token punctuation">.</span>ElementTree <span class="token keyword">as</span> ET
tree <span class="token operator">=</span> ET<span class="token punctuation">.</span>parse<span class="token punctuation">(</span><span class="token string">"yourXMLfile.xml"</span><span class="token punctuation">)</span>
root <span class="token operator">=</span> tree<span class="token punctuation">.</span>getroot<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><p>There are a few ways to search through the tree. First is by iteration:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">for</span> child <span class="token keyword">in</span> root<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>child<span class="token punctuation">.</span>tag<span class="token punctuation">,</span> child<span class="token punctuation">.</span>attrib<span class="token punctuation">)</span>
</code></pre></div><p>Otherwise you can reference specific locations like a list:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">print</span><span class="token punctuation">(</span>root<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">)</span>
</code></pre></div><p>To search for specific tags by name, use the <code>.find</code> or <code>.findall</code>:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">print</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">"myTag"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>root<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"myOtherTag"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="modifying-an-xml-file"><a href="#modifying-an-xml-file" class="header-anchor">#</a> Modifying an XML File</h2> <p>Import Element Tree module and open xml file, get an xml element</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> xml<span class="token punctuation">.</span>etree<span class="token punctuation">.</span>ElementTree <span class="token keyword">as</span> ET
tree <span class="token operator">=</span> ET<span class="token punctuation">.</span>parse<span class="token punctuation">(</span><span class="token string">'sample.xml'</span><span class="token punctuation">)</span>
root<span class="token operator">=</span>tree<span class="token punctuation">.</span>getroot<span class="token punctuation">(</span><span class="token punctuation">)</span>
element <span class="token operator">=</span> root<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment">#get first child of root element</span>
</code></pre></div><p>Element object can be manipulated by changing its fields, adding and modifying attributes, adding and removing children</p> <div class="language-py extra-class"><pre class="language-py"><code>element<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">'attribute_name'</span><span class="token punctuation">,</span> <span class="token string">'attribute_value'</span><span class="token punctuation">)</span> <span class="token comment">#set the attribute to xml element</span>
element<span class="token punctuation">.</span>text<span class="token operator">=</span><span class="token string">"string_text"</span>
</code></pre></div><p>If you want to remove an element use Element.remove() method</p> <div class="language-py extra-class"><pre class="language-py"><code>root<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>element<span class="token punctuation">)</span>
</code></pre></div><p>ElementTree.write() method used to output xml object to xml files.</p> <div class="language-py extra-class"><pre class="language-py"><code>tree<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">'output.xml'</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="create-and-build-xml-documents"><a href="#create-and-build-xml-documents" class="header-anchor">#</a> Create and Build XML Documents</h2> <p>Import Element Tree module</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> xml<span class="token punctuation">.</span>etree<span class="token punctuation">.</span>ElementTree <span class="token keyword">as</span> ET
</code></pre></div><p>Element() function is used to create XML elements</p> <div class="language-py extra-class"><pre class="language-py"><code>p<span class="token operator">=</span>ET<span class="token punctuation">.</span>Element<span class="token punctuation">(</span><span class="token string">'parent'</span><span class="token punctuation">)</span>
</code></pre></div><p>SubElement() function used to create sub-elements to a give element</p> <div class="language-py extra-class"><pre class="language-py"><code>c <span class="token operator">=</span> ET<span class="token punctuation">.</span>SubElement<span class="token punctuation">(</span>p<span class="token punctuation">,</span> <span class="token string">'child1'</span><span class="token punctuation">)</span>
</code></pre></div><p>dump() function is used to dump xml elements.</p> <div class="language-py extra-class"><pre class="language-py"><code>ET<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>p<span class="token punctuation">)</span>
<span class="token comment"># Output will be like this</span>
<span class="token comment">#<parent><child1 /></parent></span>
</code></pre></div><p>If you want to save to a file create a xml tree with ElementTree() function and to save to a file use write() method</p> <div class="language-py extra-class"><pre class="language-py"><code>tree <span class="token operator">=</span> ET<span class="token punctuation">.</span>ElementTree<span class="token punctuation">(</span>p<span class="token punctuation">)</span>
tree<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">"output.xml"</span><span class="token punctuation">)</span>
</code></pre></div><p>Comment() function is used to insert comments in xml file.</p> <div class="language-py extra-class"><pre class="language-py"><code>comment <span class="token operator">=</span> ET<span class="token punctuation">.</span>Comment<span class="token punctuation">(</span><span class="token string">'user comment'</span><span class="token punctuation">)</span>
p<span class="token punctuation">.</span>append<span class="token punctuation">(</span>comment<span class="token punctuation">)</span> <span class="token comment">#this comment will be appended to parent element</span>
</code></pre></div><h2 id="opening-and-reading-large-xml-files-using-iterparse-incremental-parsing"><a href="#opening-and-reading-large-xml-files-using-iterparse-incremental-parsing" class="header-anchor">#</a> Opening and reading large XML files using iterparse (incremental parsing)</h2> <p>Sometimes we don't want to load the entire XML file in order to get the information we need. In these instances, being able to incrementally load the relevant sections and then delete them when we are finished is useful. With the iterparse function you can edit the element tree that is stored while parsing the XML.</p> <p>Import the ElementTree object:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> xml<span class="token punctuation">.</span>etree<span class="token punctuation">.</span>ElementTree <span class="token keyword">as</span> ET
</code></pre></div><p>Open the .xml file and iterate over all the elements:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">for</span> event<span class="token punctuation">,</span> elem <span class="token keyword">in</span> ET<span class="token punctuation">.</span>iterparse<span class="token punctuation">(</span><span class="token string">"yourXMLfile.xml"</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> do something <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre></div><p>Alternatively, we can only look for specific events, such as start/end tags or namespaces.
If this option is omitted (as above), only "end" events are returned:</p> <div class="language-py extra-class"><pre class="language-py"><code>events<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"start"</span><span class="token punctuation">,</span> <span class="token string">"end"</span><span class="token punctuation">,</span> <span class="token string">"start-ns"</span><span class="token punctuation">,</span> <span class="token string">"end-ns"</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> event<span class="token punctuation">,</span> elem <span class="token keyword">in</span> ET<span class="token punctuation">.</span>iterparse<span class="token punctuation">(</span><span class="token string">"yourXMLfile.xml"</span><span class="token punctuation">,</span> events<span class="token operator">=</span>events<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> do something <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre></div><p>Here is the complete example showing how to clear elements from the in-memory tree when we are finished with them:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">for</span> event<span class="token punctuation">,</span> elem <span class="token keyword">in</span> ET<span class="token punctuation">.</span>iterparse<span class="token punctuation">(</span><span class="token string">"yourXMLfile.xml"</span><span class="token punctuation">,</span> events<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"start"</span><span class="token punctuation">,</span><span class="token string">"end"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> elem<span class="token punctuation">.</span>tag <span class="token operator">==</span> <span class="token string">"record_tag"</span> <span class="token keyword">and</span> event <span class="token operator">==</span> <span class="token string">"end"</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> elem<span class="token punctuation">.</span>text
elem<span class="token punctuation">.</span>clear<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> do something <span class="token keyword">else</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre></div><h2 id="searching-the-xml-with-xpath"><a href="#searching-the-xml-with-xpath" class="header-anchor">#</a> Searching the XML with XPath</h2> <p>Starting with version 2.7 <code>ElementTree</code> has a better support for XPath queries. XPath is a syntax to enable you to navigate through an xml like SQL is used to search through a database. Both <code>find</code> and <code>findall</code> functions support XPath. The xml below will be used for this example</p> <div class="language- extra-class"><pre class="language-text"><code>
<Catalog>
<Books>
<Book id="1" price="7.95">
<Title>Do Androids Dream of Electric Sheep?</Title>
<Author>Philip K. Dick</Author>
</Book>
<Book id="5" price="5.95">
<Title>The Colour of Magic</Title>
<Author>Terry Pratchett</Author>
</Book>
<Book id="7" price="6.95">
<Title>The Eye of The World</Title>
<Author>Robert Jordan</Author>
</Book>
</Books>
</Catalog>
</code></pre></div><p>Searching for all books:</p> <div class="language-py extra-class"><pre class="language-py"><code><span class="token keyword">import</span> xml<span class="token punctuation">.</span>etree<span class="token punctuation">.</span>cElementTree <span class="token keyword">as</span> ET
tree <span class="token operator">=</span> ET<span class="token punctuation">.</span>parse<span class="token punctuation">(</span><span class="token string">'sample.xml'</span><span class="token punctuation">)</span>
tree<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">'Books/Book'</span><span class="token punctuation">)</span>
</code></pre></div><p>Searching for the book with title = 'The Colour of Magic':</p> <div class="language-py extra-class"><pre class="language-py"><code>tree<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"Books/Book[Title='The Colour of Magic']"</span><span class="token punctuation">)</span>
<span class="token comment"># always use '' in the right side of the comparison</span>
</code></pre></div><p>Searching for the book with id = 5:</p> <div class="language-py extra-class"><pre class="language-py"><code>tree<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"Books/Book[@id='5']"</span><span class="token punctuation">)</span>
<span class="token comment"># searches with xml attributes must have '@' before the name</span>
</code></pre></div><p>Search for the second book:</p> <div class="language-py extra-class"><pre class="language-py"><code>tree<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"Books/Book[2]"</span><span class="token punctuation">)</span>
<span class="token comment"># indexes starts at 1, not 0</span>
</code></pre></div><p>Search for the last book:</p> <div class="language-py extra-class"><pre class="language-py"><code>tree<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"Books/Book[last()]"</span><span class="token punctuation">)</span>
<span class="token comment"># 'last' is the only xpath function allowed in ElementTree</span>
</code></pre></div><p>Search for all authors:</p> <div class="language-py extra-class"><pre class="language-py"><code>tree<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">".//Author"</span><span class="token punctuation">)</span>
<span class="token comment">#searches with // must use a relative path</span>
</code></pre></div><h4 id="remarks"><a href="#remarks" class="header-anchor">#</a> Remarks</h4> <p>Not all elements of the XML input will end up as elements of the parsed tree. Currently, this module skips over any XML comments, processing instructions, and document type declarations in the input. Nevertheless, trees built using this module’s API rather than parsing from XML text can have comments and processing instructions in them; they will be included when generating XML output.</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/python/manipulating-xml.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/python/html-parsing.html" class="prev">
HTML Parsing
</a></span> <span class="next"><a href="/python/python-requests-post.html">
Python Requests Post
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/2742.d9f697d0.js" defer></script>
</body>
</html>