Linux cpanel.rrshost.in 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64
Apache
: 109.123.238.221 | : 172.69.130.94
128 Domain
8.2.28
aev999
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python2.7 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
63.71
KB
-rw-r--r--
__builtin__.html
11.79
KB
-rw-r--r--
__future__.html
15.58
KB
-rw-r--r--
__main__.html
8.33
KB
-rw-r--r--
_winreg.html
73.99
KB
-rw-r--r--
abc.html
26.82
KB
-rw-r--r--
aepack.html
15.47
KB
-rw-r--r--
aetools.html
18.79
KB
-rw-r--r--
aetypes.html
27.08
KB
-rw-r--r--
aifc.html
29.59
KB
-rw-r--r--
al.html
22.13
KB
-rw-r--r--
allos.html
35.48
KB
-rw-r--r--
anydbm.html
19.31
KB
-rw-r--r--
archiving.html
10.64
KB
-rw-r--r--
argparse.html
269.59
KB
-rw-r--r--
array.html
35.35
KB
-rw-r--r--
ast.html
42.42
KB
-rw-r--r--
asynchat.html
35.84
KB
-rw-r--r--
asyncore.html
43.22
KB
-rw-r--r--
atexit.html
19.23
KB
-rw-r--r--
audioop.html
41.32
KB
-rw-r--r--
autogil.html
9.77
KB
-rw-r--r--
base64.html
24.8
KB
-rw-r--r--
basehttpserver.html
40.6
KB
-rw-r--r--
bastion.html
13.02
KB
-rw-r--r--
bdb.html
50.06
KB
-rw-r--r--
binascii.html
26.64
KB
-rw-r--r--
binhex.html
12.66
KB
-rw-r--r--
bisect.html
28.52
KB
-rw-r--r--
bsddb.html
32.02
KB
-rw-r--r--
bz2.html
31.92
KB
-rw-r--r--
calendar.html
50.16
KB
-rw-r--r--
carbon.html
54.14
KB
-rw-r--r--
cd.html
35.24
KB
-rw-r--r--
cgi.html
58.4
KB
-rw-r--r--
cgihttpserver.html
15.12
KB
-rw-r--r--
cgitb.html
13.5
KB
-rw-r--r--
chunk.html
17.41
KB
-rw-r--r--
cmath.html
32.41
KB
-rw-r--r--
cmd.html
32.69
KB
-rw-r--r--
code.html
28.92
KB
-rw-r--r--
codecs.html
125.92
KB
-rw-r--r--
codeop.html
16.42
KB
-rw-r--r--
collections.html
150.36
KB
-rw-r--r--
colorpicker.html
9.02
KB
-rw-r--r--
colorsys.html
14.38
KB
-rw-r--r--
commands.html
16.63
KB
-rw-r--r--
compileall.html
21.18
KB
-rw-r--r--
compiler.html
78.14
KB
-rw-r--r--
configparser.html
73.92
KB
-rw-r--r--
constants.html
14.68
KB
-rw-r--r--
contextlib.html
22.91
KB
-rw-r--r--
cookie.html
45.38
KB
-rw-r--r--
cookielib.html
102.78
KB
-rw-r--r--
copy.html
14.21
KB
-rw-r--r--
copy_reg.html
15.82
KB
-rw-r--r--
crypt.html
11.62
KB
-rw-r--r--
crypto.html
8.35
KB
-rw-r--r--
csv.html
82.79
KB
-rw-r--r--
ctypes.html
280.57
KB
-rw-r--r--
curses.ascii.html
28.97
KB
-rw-r--r--
curses.html
200.94
KB
-rw-r--r--
curses.panel.html
18.43
KB
-rw-r--r--
custominterp.html
8.59
KB
-rw-r--r--
datatypes.html
18.84
KB
-rw-r--r--
datetime.html
261.02
KB
-rw-r--r--
dbhash.html
18.4
KB
-rw-r--r--
dbm.html
14.75
KB
-rw-r--r--
debug.html
11.28
KB
-rw-r--r--
decimal.html
239.14
KB
-rw-r--r--
development.html
15.27
KB
-rw-r--r--
difflib.html
96.93
KB
-rw-r--r--
dircache.html
13.66
KB
-rw-r--r--
dis.html
96.52
KB
-rw-r--r--
distribution.html
8.27
KB
-rw-r--r--
distutils.html
10.97
KB
-rw-r--r--
dl.html
19.27
KB
-rw-r--r--
doctest.html
193.18
KB
-rw-r--r--
docxmlrpcserver.html
20.02
KB
-rw-r--r--
dumbdbm.html
16.74
KB
-rw-r--r--
dummy_thread.html
11.04
KB
-rw-r--r--
dummy_threading.html
9.96
KB
-rw-r--r--
easydialogs.html
38.83
KB
-rw-r--r--
email-examples.html
47.2
KB
-rw-r--r--
email.charset.html
32.45
KB
-rw-r--r--
email.encoders.html
14.09
KB
-rw-r--r--
email.errors.html
19.2
KB
-rw-r--r--
email.generator.html
24.78
KB
-rw-r--r--
email.header.html
31.12
KB
-rw-r--r--
email.html
55.3
KB
-rw-r--r--
email.iterators.html
14.22
KB
-rw-r--r--
email.message.html
74.91
KB
-rw-r--r--
email.mime.html
34.51
KB
-rw-r--r--
email.parser.html
37.09
KB
-rw-r--r--
email.utils.html
30.04
KB
-rw-r--r--
ensurepip.html
20.21
KB
-rw-r--r--
errno.html
52.31
KB
-rw-r--r--
exceptions.html
73.4
KB
-rw-r--r--
fcntl.html
27.85
KB
-rw-r--r--
filecmp.html
26.49
KB
-rw-r--r--
fileformats.html
10.21
KB
-rw-r--r--
fileinput.html
29.36
KB
-rw-r--r--
filesys.html
11.55
KB
-rw-r--r--
fl.html
71.58
KB
-rw-r--r--
fm.html
15
KB
-rw-r--r--
fnmatch.html
17.78
KB
-rw-r--r--
formatter.html
43.91
KB
-rw-r--r--
fpectl.html
17.84
KB
-rw-r--r--
fpformat.html
12.65
KB
-rw-r--r--
fractions.html
27.17
KB
-rw-r--r--
framework.html
44.24
KB
-rw-r--r--
frameworks.html
8.11
KB
-rw-r--r--
ftplib.html
56.56
KB
-rw-r--r--
functions.html
216.84
KB
-rw-r--r--
functools.html
31.06
KB
-rw-r--r--
future_builtins.html
15.9
KB
-rw-r--r--
gc.html
30.74
KB
-rw-r--r--
gdbm.html
19.35
KB
-rw-r--r--
gensuitemodule.html
14.54
KB
-rw-r--r--
getopt.html
26.56
KB
-rw-r--r--
getpass.html
12.5
KB
-rw-r--r--
gettext.html
91.32
KB
-rw-r--r--
gl.html
26.71
KB
-rw-r--r--
glob.html
15.42
KB
-rw-r--r--
grp.html
12.33
KB
-rw-r--r--
gzip.html
21.95
KB
-rw-r--r--
hashlib.html
27.4
KB
-rw-r--r--
heapq.html
36.72
KB
-rw-r--r--
hmac.html
15.97
KB
-rw-r--r--
hotshot.html
22.69
KB
-rw-r--r--
htmllib.html
29.9
KB
-rw-r--r--
htmlparser.html
45.11
KB
-rw-r--r--
httplib.html
78.55
KB
-rw-r--r--
i18n.html
10.61
KB
-rw-r--r--
ic.html
22.03
KB
-rw-r--r--
idle.html
45.74
KB
-rw-r--r--
imageop.html
20.94
KB
-rw-r--r--
imaplib.html
69.9
KB
-rw-r--r--
imgfile.html
14.91
KB
-rw-r--r--
imghdr.html
13.43
KB
-rw-r--r--
imp.html
41.48
KB
-rw-r--r--
importlib.html
10.27
KB
-rw-r--r--
imputil.html
36.11
KB
-rw-r--r--
index.html
82.54
KB
-rw-r--r--
inspect.html
64.31
KB
-rw-r--r--
internet.html
26.77
KB
-rw-r--r--
intro.html
9.76
KB
-rw-r--r--
io.html
122.3
KB
-rw-r--r--
ipc.html
17.18
KB
-rw-r--r--
itertools.html
129.91
KB
-rw-r--r--
jpeg.html
15.36
KB
-rw-r--r--
json.html
79.09
KB
-rw-r--r--
keyword.html
9.27
KB
-rw-r--r--
language.html
12.35
KB
-rw-r--r--
linecache.html
12.7
KB
-rw-r--r--
locale.html
68.54
KB
-rw-r--r--
logging.config.html
82.23
KB
-rw-r--r--
logging.handlers.html
91.96
KB
-rw-r--r--
logging.html
123.15
KB
-rw-r--r--
mac.html
24.49
KB
-rw-r--r--
macos.html
18.51
KB
-rw-r--r--
macosa.html
14.55
KB
-rw-r--r--
macostools.html
19.67
KB
-rw-r--r--
macpath.html
9.19
KB
-rw-r--r--
mailbox.html
185.53
KB
-rw-r--r--
mailcap.html
15.3
KB
-rw-r--r--
markup.html
20.37
KB
-rw-r--r--
marshal.html
19.88
KB
-rw-r--r--
math.html
50.52
KB
-rw-r--r--
md5.html
16.65
KB
-rw-r--r--
mhlib.html
30.04
KB
-rw-r--r--
mimetools.html
23.84
KB
-rw-r--r--
mimetypes.html
35.82
KB
-rw-r--r--
mimewriter.html
18.19
KB
-rw-r--r--
mimify.html
16.96
KB
-rw-r--r--
miniaeframe.html
14.5
KB
-rw-r--r--
misc.html
7.77
KB
-rw-r--r--
mm.html
10.19
KB
-rw-r--r--
mmap.html
33.52
KB
-rw-r--r--
modulefinder.html
19.85
KB
-rw-r--r--
modules.html
9.68
KB
-rw-r--r--
msilib.html
72.78
KB
-rw-r--r--
msvcrt.html
24.1
KB
-rw-r--r--
multifile.html
28.82
KB
-rw-r--r--
multiprocessing.html
423.97
KB
-rw-r--r--
mutex.html
13.56
KB
-rw-r--r--
netdata.html
19.28
KB
-rw-r--r--
netrc.html
15.51
KB
-rw-r--r--
new.html
16.1
KB
-rw-r--r--
nis.html
13.19
KB
-rw-r--r--
nntplib.html
51.08
KB
-rw-r--r--
numbers.html
40.91
KB
-rw-r--r--
numeric.html
14.85
KB
-rw-r--r--
operator.html
118.32
KB
-rw-r--r--
optparse.html
251.08
KB
-rw-r--r--
os.html
270.49
KB
-rw-r--r--
os.path.html
48.87
KB
-rw-r--r--
ossaudiodev.html
49.79
KB
-rw-r--r--
othergui.html
9.98
KB
-rw-r--r--
parser.html
44.52
KB
-rw-r--r--
pdb.html
41.44
KB
-rw-r--r--
persistence.html
16.25
KB
-rw-r--r--
pickle.html
107.94
KB
-rw-r--r--
pickletools.html
13.21
KB
-rw-r--r--
pipes.html
21.85
KB
-rw-r--r--
pkgutil.html
30.59
KB
-rw-r--r--
platform.html
40.14
KB
-rw-r--r--
plistlib.html
20.96
KB
-rw-r--r--
popen2.html
30.13
KB
-rw-r--r--
poplib.html
27.93
KB
-rw-r--r--
posix.html
17.27
KB
-rw-r--r--
posixfile.html
23.68
KB
-rw-r--r--
pprint.html
36.42
KB
-rw-r--r--
profile.html
76.65
KB
-rw-r--r--
pty.html
11.42
KB
-rw-r--r--
pwd.html
13.53
KB
-rw-r--r--
py_compile.html
13.33
KB
-rw-r--r--
pyclbr.html
18.14
KB
-rw-r--r--
pydoc.html
14.33
KB
-rw-r--r--
pyexpat.html
93.28
KB
-rw-r--r--
python.html
13.53
KB
-rw-r--r--
queue.html
29.64
KB
-rw-r--r--
quopri.html
14.5
KB
-rw-r--r--
random.html
47.78
KB
-rw-r--r--
re.html
163.11
KB
-rw-r--r--
readline.html
41.2
KB
-rw-r--r--
repr.html
24.57
KB
-rw-r--r--
resource.html
30.86
KB
-rw-r--r--
restricted.html
12.69
KB
-rw-r--r--
rexec.html
43.59
KB
-rw-r--r--
rfc822.html
51.25
KB
-rw-r--r--
rlcompleter.html
15.29
KB
-rw-r--r--
robotparser.html
15.01
KB
-rw-r--r--
runpy.html
23.55
KB
-rw-r--r--
sched.html
21.76
KB
-rw-r--r--
scrolledtext.html
10.67
KB
-rw-r--r--
select.html
50.17
KB
-rw-r--r--
sets.html
39.92
KB
-rw-r--r--
sgi.html
11.09
KB
-rw-r--r--
sgmllib.html
38.77
KB
-rw-r--r--
sha.html
14.55
KB
-rw-r--r--
shelve.html
32.93
KB
-rw-r--r--
shlex.html
38.43
KB
-rw-r--r--
shutil.html
49.49
KB
-rw-r--r--
signal.html
36.52
KB
-rw-r--r--
simplehttpserver.html
21.57
KB
-rw-r--r--
simplexmlrpcserver.html
40.41
KB
-rw-r--r--
site.html
29
KB
-rw-r--r--
smtpd.html
15.64
KB
-rw-r--r--
smtplib.html
51.86
KB
-rw-r--r--
sndhdr.html
12.05
KB
-rw-r--r--
socket.html
126.94
KB
-rw-r--r--
socketserver.html
81.38
KB
-rw-r--r--
someos.html
17.09
KB
-rw-r--r--
spwd.html
12.25
KB
-rw-r--r--
sqlite3.html
154.12
KB
-rw-r--r--
ssl.html
214.5
KB
-rw-r--r--
stat.html
41.11
KB
-rw-r--r--
statvfs.html
13.24
KB
-rw-r--r--
stdtypes.html
309.83
KB
-rw-r--r--
string.html
129.58
KB
-rw-r--r--
stringio.html
21.1
KB
-rw-r--r--
stringprep.html
21.4
KB
-rw-r--r--
strings.html
16.42
KB
-rw-r--r--
struct.html
47.68
KB
-rw-r--r--
subprocess.html
117
KB
-rw-r--r--
sun.html
7.83
KB
-rw-r--r--
sunau.html
35.43
KB
-rw-r--r--
sunaudio.html
21.05
KB
-rw-r--r--
symbol.html
9.06
KB
-rw-r--r--
symtable.html
29.03
KB
-rw-r--r--
sys.html
118.12
KB
-rw-r--r--
sysconfig.html
28.8
KB
-rw-r--r--
syslog.html
21.29
KB
-rw-r--r--
tabnanny.html
12.78
KB
-rw-r--r--
tarfile.html
99.54
KB
-rw-r--r--
telnetlib.html
30.46
KB
-rw-r--r--
tempfile.html
35.02
KB
-rw-r--r--
termios.html
19.22
KB
-rw-r--r--
test.html
61.42
KB
-rw-r--r--
textwrap.html
32.28
KB
-rw-r--r--
thread.html
22.67
KB
-rw-r--r--
threading.html
92.04
KB
-rw-r--r--
time.html
65.55
KB
-rw-r--r--
timeit.html
44.38
KB
-rw-r--r--
tix.html
56.73
KB
-rw-r--r--
tk.html
26.76
KB
-rw-r--r--
tkinter.html
86.67
KB
-rw-r--r--
token.html
27.31
KB
-rw-r--r--
tokenize.html
21.69
KB
-rw-r--r--
trace.html
33.91
KB
-rw-r--r--
traceback.html
44.22
KB
-rw-r--r--
ttk.html
123.83
KB
-rw-r--r--
tty.html
10.95
KB
-rw-r--r--
turtle.html
246.17
KB
-rw-r--r--
types.html
33.66
KB
-rw-r--r--
undoc.html
25.54
KB
-rw-r--r--
unicodedata.html
22.64
KB
-rw-r--r--
unittest.html
246.75
KB
-rw-r--r--
unix.html
11.9
KB
-rw-r--r--
urllib.html
71.61
KB
-rw-r--r--
urllib2.html
128.77
KB
-rw-r--r--
urlparse.html
49.04
KB
-rw-r--r--
user.html
13.41
KB
-rw-r--r--
userdict.html
32.96
KB
-rw-r--r--
uu.html
13.25
KB
-rw-r--r--
uuid.html
33.55
KB
-rw-r--r--
warnings.html
51.86
KB
-rw-r--r--
wave.html
29.08
KB
-rw-r--r--
weakref.html
41.14
KB
-rw-r--r--
webbrowser.html
29.53
KB
-rw-r--r--
whichdb.html
10.44
KB
-rw-r--r--
windows.html
10.37
KB
-rw-r--r--
winsound.html
22.56
KB
-rw-r--r--
wsgiref.html
94.38
KB
-rw-r--r--
xdrlib.html
38.05
KB
-rw-r--r--
xml.dom.html
110.39
KB
-rw-r--r--
xml.dom.minidom.html
45.23
KB
-rw-r--r--
xml.dom.pulldom.html
16.32
KB
-rw-r--r--
xml.etree.elementtree.html
124.2
KB
-rw-r--r--
xml.html
18.76
KB
-rw-r--r--
xml.sax.handler.html
47.5
KB
-rw-r--r--
xml.sax.html
26.04
KB
-rw-r--r--
xml.sax.reader.html
51.4
KB
-rw-r--r--
xml.sax.utils.html
18.11
KB
-rw-r--r--
xmlrpclib.html
71.79
KB
-rw-r--r--
zipfile.html
67.26
KB
-rw-r--r--
zipimport.html
24.32
KB
-rw-r--r--
zlib.html
34.41
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : xml.etree.elementtree.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>19.7. xml.etree.ElementTree — The ElementTree XML API — Python 2.7.18 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/classic.css" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.18 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="19.8. xml.dom — The Document Object Model API" href="xml.dom.html" /> <link rel="prev" title="19.5. XML Processing Modules" href="xml.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/xml.etree.elementtree.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;"> This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>. You should install the python3 and python3-doc packages and read the <a href="file:///usr/share/doc/python3-doc/html/library/xml.etree.elementtree.html"> Python documentation for the Python3 version packaged in this release</a>. </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="xml.dom.html" title="19.8. xml.dom — The Document Object Model API" accesskey="N">next</a> |</li> <li class="right" > <a href="xml.html" title="19.5. XML Processing Modules" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="markup.html" accesskey="U"><span class="section-number">19. </span>Structured Markup Processing Tools</a> »</li> <li class="nav-item nav-item-this"><a href=""><span class="section-number">19.7. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-xml.etree.ElementTree"> <span id="xml-etree-elementtree-the-elementtree-xml-api"></span><h1><span class="section-number">19.7. </span><a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> — The ElementTree XML API<a class="headerlink" href="#module-xml.etree.ElementTree" title="Permalink to this headline">¶</a></h1> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.5.</span></p> </div> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/2.7/Lib/xml/etree/ElementTree.py">Lib/xml/etree/ElementTree.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> type is a flexible container object, designed to store hierarchical data structures in memory. The type can be described as a cross between a list and a dictionary.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see <a class="reference internal" href="xml.html#xml-vulnerabilities"><span class="std std-ref">XML vulnerabilities</span></a>.</p> </div> <p>Each element has a number of properties associated with it:</p> <ul class="simple"> <li><p>a tag which is a string identifying what kind of data this element represents (the element type, in other words).</p></li> <li><p>a number of attributes, stored in a Python dictionary.</p></li> <li><p>a text string.</p></li> <li><p>an optional tail string.</p></li> <li><p>a number of child elements, stored in a Python sequence</p></li> </ul> <p>To create an element instance, use the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> constructor or the <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal notranslate"><span class="pre">SubElement()</span></code></a> factory function.</p> <p>The <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> class can be used to wrap an element structure, and convert it from and to XML.</p> <p>A C implementation of this API is available as <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code>.</p> <p>See <a class="reference external" href="http://effbot.org/zone/element-index.htm">http://effbot.org/zone/element-index.htm</a> for tutorials and links to other docs. Fredrik Lundh’s page is also the location of the development version of the xml.etree.ElementTree.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.7: </span>The ElementTree API is updated to 1.3. For more information, see <a class="reference external" href="http://effbot.org/zone/elementtree-13-intro.htm">Introducing ElementTree 1.3</a>.</p> </div> <section id="tutorial"> <h2><span class="section-number">19.7.1. </span>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2> <p>This is a short tutorial for using <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> (<code class="docutils literal notranslate"><span class="pre">ET</span></code> in short). The goal is to demonstrate some of the building blocks and basic concepts of the module.</p> <section id="xml-tree-and-elements"> <h3><span class="section-number">19.7.1.1. </span>XML tree and elements<a class="headerlink" href="#xml-tree-and-elements" title="Permalink to this headline">¶</a></h3> <p>XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. <code class="docutils literal notranslate"><span class="pre">ET</span></code> has two classes for this purpose - <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> represents the whole XML document as a tree, and <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> level. Interactions with a single XML element and its sub-elements are done on the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> level.</p> </section> <section id="parsing-xml"> <span id="elementtree-parsing-xml"></span><h3><span class="section-number">19.7.1.2. </span>Parsing XML<a class="headerlink" href="#parsing-xml" title="Permalink to this headline">¶</a></h3> <p>We’ll be using the following XML document as the sample data for this section:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span> <span class="nt"><data></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Liechtenstein"</span><span class="nt">></span> <span class="nt"><rank></span>1<span class="nt"></rank></span> <span class="nt"><year></span>2008<span class="nt"></year></span> <span class="nt"><gdppc></span>141100<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Austria"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Switzerland"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Singapore"</span><span class="nt">></span> <span class="nt"><rank></span>4<span class="nt"></rank></span> <span class="nt"><year></span>2011<span class="nt"></year></span> <span class="nt"><gdppc></span>59900<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Malaysia"</span> <span class="na">direction=</span><span class="s">"N"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Panama"</span><span class="nt">></span> <span class="nt"><rank></span>68<span class="nt"></rank></span> <span class="nt"><year></span>2011<span class="nt"></year></span> <span class="nt"><gdppc></span>13600<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Costa Rica"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Colombia"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"></data></span> </pre></div> </div> <p>We have a number of ways to import the data. Reading the file from disk:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span> <span class="n">tree</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">'country_data.xml'</span><span class="p">)</span> <span class="n">root</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">getroot</span><span class="p">()</span> </pre></div> </div> <p>Reading the data from a string:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">country_data_as_string</span><span class="p">)</span> </pre></div> </div> <p><a class="reference internal" href="#xml.etree.ElementTree.fromstring" title="xml.etree.ElementTree.fromstring"><code class="xref py py-func docutils literal notranslate"><span class="pre">fromstring()</span></code></a> parses XML from a string directly into an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a>, which is the root element of the parsed tree. Other parsing functions may create an <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a>. Check the documentation to be sure.</p> <p>As an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a>, <code class="docutils literal notranslate"><span class="pre">root</span></code> has a tag and a dictionary of attributes:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">root</span><span class="o">.</span><span class="n">tag</span> <span class="go">'data'</span> <span class="gp">>>> </span><span class="n">root</span><span class="o">.</span><span class="n">attrib</span> <span class="go">{}</span> </pre></div> </div> <p>It also has children nodes over which we can iterate:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">root</span><span class="p">:</span> <span class="gp">... </span> <span class="nb">print</span> <span class="n">child</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">child</span><span class="o">.</span><span class="n">attrib</span> <span class="gp">...</span> <span class="go">country {'name': 'Liechtenstein'}</span> <span class="go">country {'name': 'Singapore'}</span> <span class="go">country {'name': 'Panama'}</span> </pre></div> </div> <p>Children are nested, and we can access specific child nodes by index:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">root</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">text</span> <span class="go">'2008'</span> </pre></div> </div> </section> <section id="finding-interesting-elements"> <h3><span class="section-number">19.7.1.3. </span>Finding interesting elements<a class="headerlink" href="#finding-interesting-elements" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> has some useful methods that help iterate recursively over all the sub-tree below it (its children, their children, and so on). For example, <a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.iter()</span></code></a>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">neighbor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s1">'neighbor'</span><span class="p">):</span> <span class="gp">... </span> <span class="nb">print</span> <span class="n">neighbor</span><span class="o">.</span><span class="n">attrib</span> <span class="gp">...</span> <span class="go">{'name': 'Austria', 'direction': 'E'}</span> <span class="go">{'name': 'Switzerland', 'direction': 'W'}</span> <span class="go">{'name': 'Malaysia', 'direction': 'N'}</span> <span class="go">{'name': 'Costa Rica', 'direction': 'W'}</span> <span class="go">{'name': 'Colombia', 'direction': 'E'}</span> </pre></div> </div> <p><a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.findall()</span></code></a> finds only elements with a tag which are direct children of the current element. <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.find()</span></code></a> finds the <em>first</em> child with a particular tag, and <a class="reference internal" href="#xml.etree.ElementTree.Element.text" title="xml.etree.ElementTree.Element.text"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Element.text</span></code></a> accesses the element’s text content. <a class="reference internal" href="#xml.etree.ElementTree.Element.get" title="xml.etree.ElementTree.Element.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.get()</span></code></a> accesses the element’s attributes:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">country</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'country'</span><span class="p">):</span> <span class="gp">... </span> <span class="n">rank</span> <span class="o">=</span> <span class="n">country</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'rank'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span> <span class="gp">... </span> <span class="n">name</span> <span class="o">=</span> <span class="n">country</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'name'</span><span class="p">)</span> <span class="gp">... </span> <span class="nb">print</span> <span class="n">name</span><span class="p">,</span> <span class="n">rank</span> <span class="gp">...</span> <span class="go">Liechtenstein 1</span> <span class="go">Singapore 4</span> <span class="go">Panama 68</span> </pre></div> </div> <p>More sophisticated specification of which elements to look for is possible by using <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">XPath</span></a>.</p> </section> <section id="modifying-an-xml-file"> <h3><span class="section-number">19.7.1.4. </span>Modifying an XML File<a class="headerlink" href="#modifying-an-xml-file" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> provides a simple way to build XML documents and write them to files. The <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ElementTree.write()</span></code></a> method serves this purpose.</p> <p>Once created, an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> object may be manipulated by directly changing its fields (such as <a class="reference internal" href="#xml.etree.ElementTree.Element.text" title="xml.etree.ElementTree.Element.text"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Element.text</span></code></a>), adding and modifying attributes (<a class="reference internal" href="#xml.etree.ElementTree.Element.set" title="xml.etree.ElementTree.Element.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.set()</span></code></a> method), as well as adding new children (for example with <a class="reference internal" href="#xml.etree.ElementTree.Element.append" title="xml.etree.ElementTree.Element.append"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.append()</span></code></a>).</p> <p>Let’s say we want to add one to each country’s rank, and add an <code class="docutils literal notranslate"><span class="pre">updated</span></code> attribute to the rank element:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">rank</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s1">'rank'</span><span class="p">):</span> <span class="gp">... </span> <span class="n">new_rank</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">rank</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> <span class="gp">... </span> <span class="n">rank</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">new_rank</span><span class="p">)</span> <span class="gp">... </span> <span class="n">rank</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s1">'updated'</span><span class="p">,</span> <span class="s1">'yes'</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'output.xml'</span><span class="p">)</span> </pre></div> </div> <p>Our XML now looks like this:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span> <span class="nt"><data></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Liechtenstein"</span><span class="nt">></span> <span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>2<span class="nt"></rank></span> <span class="nt"><year></span>2008<span class="nt"></year></span> <span class="nt"><gdppc></span>141100<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Austria"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Switzerland"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Singapore"</span><span class="nt">></span> <span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>5<span class="nt"></rank></span> <span class="nt"><year></span>2011<span class="nt"></year></span> <span class="nt"><gdppc></span>59900<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Malaysia"</span> <span class="na">direction=</span><span class="s">"N"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Panama"</span><span class="nt">></span> <span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>69<span class="nt"></rank></span> <span class="nt"><year></span>2011<span class="nt"></year></span> <span class="nt"><gdppc></span>13600<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Costa Rica"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Colombia"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"></data></span> </pre></div> </div> <p>We can remove elements using <a class="reference internal" href="#xml.etree.ElementTree.Element.remove" title="xml.etree.ElementTree.Element.remove"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.remove()</span></code></a>. Let’s say we want to remove all countries with a rank higher than 50:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">country</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'country'</span><span class="p">):</span> <span class="gp">... </span> <span class="n">rank</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">country</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'rank'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="n">rank</span> <span class="o">></span> <span class="mi">50</span><span class="p">:</span> <span class="gp">... </span> <span class="n">root</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">country</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'output.xml'</span><span class="p">)</span> </pre></div> </div> <p>Our XML now looks like this:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span> <span class="nt"><data></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Liechtenstein"</span><span class="nt">></span> <span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>2<span class="nt"></rank></span> <span class="nt"><year></span>2008<span class="nt"></year></span> <span class="nt"><gdppc></span>141100<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Austria"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Switzerland"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"><country</span> <span class="na">name=</span><span class="s">"Singapore"</span><span class="nt">></span> <span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>5<span class="nt"></rank></span> <span class="nt"><year></span>2011<span class="nt"></year></span> <span class="nt"><gdppc></span>59900<span class="nt"></gdppc></span> <span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Malaysia"</span> <span class="na">direction=</span><span class="s">"N"</span><span class="nt">/></span> <span class="nt"></country></span> <span class="nt"></data></span> </pre></div> </div> </section> <section id="building-xml-documents"> <h3><span class="section-number">19.7.1.5. </span>Building XML documents<a class="headerlink" href="#building-xml-documents" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal notranslate"><span class="pre">SubElement()</span></code></a> function also provides a convenient way to create new sub-elements for a given element:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">ET</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="go"><a><b /><c><d /></c></a></span> </pre></div> </div> </section> <section id="parsing-xml-with-namespaces"> <h3><span class="section-number">19.7.1.6. </span>Parsing XML with Namespaces<a class="headerlink" href="#parsing-xml-with-namespaces" title="Permalink to this headline">¶</a></h3> <p>If the XML input has <a class="reference external" href="https://en.wikipedia.org/wiki/XML_namespace">namespaces</a>, tags and attributes with prefixes in the form <code class="docutils literal notranslate"><span class="pre">prefix:sometag</span></code> get expanded to <code class="docutils literal notranslate"><span class="pre">{uri}sometag</span></code> where the <em>prefix</em> is replaced by the full <em>URI</em>. Also, if there is a <a class="reference external" href="https://www.w3.org/TR/xml-names/#defaulting">default namespace</a>, that full URI gets prepended to all of the non-prefixed tags.</p> <p>Here is an XML example that incorporates two namespaces, one with the prefix “fictional” and the other serving as the default namespace:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span> <span class="nt"><actors</span> <span class="na">xmlns:fictional=</span><span class="s">"http://characters.example.com"</span> <span class="na">xmlns=</span><span class="s">"http://people.example.com"</span><span class="nt">></span> <span class="nt"><actor></span> <span class="nt"><name></span>John Cleese<span class="nt"></name></span> <span class="nt"><fictional:character></span>Lancelot<span class="nt"></fictional:character></span> <span class="nt"><fictional:character></span>Archie Leach<span class="nt"></fictional:character></span> <span class="nt"></actor></span> <span class="nt"><actor></span> <span class="nt"><name></span>Eric Idle<span class="nt"></name></span> <span class="nt"><fictional:character></span>Sir Robin<span class="nt"></fictional:character></span> <span class="nt"><fictional:character></span>Gunther<span class="nt"></fictional:character></span> <span class="nt"><fictional:character></span>Commander Clement<span class="nt"></fictional:character></span> <span class="nt"></actor></span> <span class="nt"></actors></span> </pre></div> </div> <p>One way to search and explore this XML example is to manually add the URI to every tag or attribute in the xpath of a <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a> or <a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">findall()</span></code></a>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">root</span> <span class="o">=</span> <span class="n">fromstring</span><span class="p">(</span><span class="n">xml_text</span><span class="p">)</span> <span class="k">for</span> <span class="n">actor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'{http://people.example.com}actor'</span><span class="p">):</span> <span class="n">name</span> <span class="o">=</span> <span class="n">actor</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'{http://people.example.com}name'</span><span class="p">)</span> <span class="nb">print</span> <span class="n">name</span><span class="o">.</span><span class="n">text</span> <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">actor</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'{http://characters.example.com}character'</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">' |-->'</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span> </pre></div> </div> <p>A better way to search the namespaced XML example is to create a dictionary with your own prefixes and use those in the search functions:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ns</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'real_person'</span><span class="p">:</span> <span class="s1">'http://people.example.com'</span><span class="p">,</span> <span class="s1">'role'</span><span class="p">:</span> <span class="s1">'http://characters.example.com'</span><span class="p">}</span> <span class="k">for</span> <span class="n">actor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'real_person:actor'</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span> <span class="n">name</span> <span class="o">=</span> <span class="n">actor</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'real_person:name'</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span> <span class="nb">print</span> <span class="n">name</span><span class="o">.</span><span class="n">text</span> <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">actor</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'role:character'</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">' |-->'</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span> </pre></div> </div> <p>These two approaches both output:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">John</span> <span class="n">Cleese</span> <span class="o">|--></span> <span class="n">Lancelot</span> <span class="o">|--></span> <span class="n">Archie</span> <span class="n">Leach</span> <span class="n">Eric</span> <span class="n">Idle</span> <span class="o">|--></span> <span class="n">Sir</span> <span class="n">Robin</span> <span class="o">|--></span> <span class="n">Gunther</span> <span class="o">|--></span> <span class="n">Commander</span> <span class="n">Clement</span> </pre></div> </div> </section> <section id="additional-resources"> <h3><span class="section-number">19.7.1.7. </span>Additional resources<a class="headerlink" href="#additional-resources" title="Permalink to this headline">¶</a></h3> <p>See <a class="reference external" href="http://effbot.org/zone/element-index.htm">http://effbot.org/zone/element-index.htm</a> for tutorials and links to other docs.</p> </section> </section> <section id="xpath-support"> <span id="elementtree-xpath"></span><h2><span class="section-number">19.7.2. </span>XPath support<a class="headerlink" href="#xpath-support" title="Permalink to this headline">¶</a></h2> <p>This module provides limited support for <a class="reference external" href="https://www.w3.org/TR/xpath">XPath expressions</a> for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.</p> <section id="example"> <h3><span class="section-number">19.7.2.1. </span>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3> <p>Here’s an example that demonstrates some of the XPath capabilities of the module. We’ll be using the <code class="docutils literal notranslate"><span class="pre">countrydata</span></code> XML document from the <a class="reference internal" href="#elementtree-parsing-xml"><span class="std std-ref">Parsing XML</span></a> section:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span> <span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">countrydata</span><span class="p">)</span> <span class="c1"># Top-level elements</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">"."</span><span class="p">)</span> <span class="c1"># All 'neighbor' grand-children of 'country' children of the top-level</span> <span class="c1"># elements</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">"./country/neighbor"</span><span class="p">)</span> <span class="c1"># Nodes with name='Singapore' that have a 'year' child</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">".//year/..[@name='Singapore']"</span><span class="p">)</span> <span class="c1"># 'year' nodes that are children of nodes with name='Singapore'</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">".//*[@name='Singapore']/year"</span><span class="p">)</span> <span class="c1"># All 'neighbor' nodes that are the second child of their parent</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">".//neighbor[2]"</span><span class="p">)</span> </pre></div> </div> </section> <section id="supported-xpath-syntax"> <h3><span class="section-number">19.7.2.2. </span>Supported XPath syntax<a class="headerlink" href="#supported-xpath-syntax" title="Permalink to this headline">¶</a></h3> <table class="docutils align-default"> <colgroup> <col style="width: 30%" /> <col style="width: 70%" /> </colgroup> <thead> <tr class="row-odd"><th class="head"><p>Syntax</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tbody> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">tag</span></code></p></td> <td><p>Selects all child elements with the given tag. For example, <code class="docutils literal notranslate"><span class="pre">spam</span></code> selects all child elements named <code class="docutils literal notranslate"><span class="pre">spam</span></code>, and <code class="docutils literal notranslate"><span class="pre">spam/egg</span></code> selects all grandchildren named <code class="docutils literal notranslate"><span class="pre">egg</span></code> in all children named <code class="docutils literal notranslate"><span class="pre">spam</span></code>.</p></td> </tr> <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">*</span></code></p></td> <td><p>Selects all child elements. For example, <code class="docutils literal notranslate"><span class="pre">*/egg</span></code> selects all grandchildren named <code class="docutils literal notranslate"><span class="pre">egg</span></code>.</p></td> </tr> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">.</span></code></p></td> <td><p>Selects the current node. This is mostly useful at the beginning of the path, to indicate that it’s a relative path.</p></td> </tr> <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">//</span></code></p></td> <td><p>Selects all subelements, on all levels beneath the current element. For example, <code class="docutils literal notranslate"><span class="pre">.//egg</span></code> selects all <code class="docutils literal notranslate"><span class="pre">egg</span></code> elements in the entire tree.</p></td> </tr> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">..</span></code></p></td> <td><p>Selects the parent element.</p></td> </tr> <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[@attrib]</span></code></p></td> <td><p>Selects all elements that have the given attribute.</p></td> </tr> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">[@attrib='value']</span></code></p></td> <td><p>Selects all elements for which the given attribute has the given value. The value cannot contain quotes.</p></td> </tr> <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[tag]</span></code></p></td> <td><p>Selects all elements that have a child named <code class="docutils literal notranslate"><span class="pre">tag</span></code>. Only immediate children are supported.</p></td> </tr> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">[tag='text']</span></code></p></td> <td><p>Selects all elements that have a child named <code class="docutils literal notranslate"><span class="pre">tag</span></code> whose complete text content, including descendants, equals the given <code class="docutils literal notranslate"><span class="pre">text</span></code>.</p></td> </tr> <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[position]</span></code></p></td> <td><p>Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression <code class="docutils literal notranslate"><span class="pre">last()</span></code> (for the last position), or a position relative to the last position (e.g. <code class="docutils literal notranslate"><span class="pre">last()-1</span></code>).</p></td> </tr> </tbody> </table> <p>Predicates (expressions within square brackets) must be preceded by a tag name, an asterisk, or another predicate. <code class="docutils literal notranslate"><span class="pre">position</span></code> predicates must be preceded by a tag name.</p> </section> </section> <section id="reference"> <h2><span class="section-number">19.7.3. </span>Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h2> <section id="functions"> <span id="elementtree-functions"></span><h3><span class="section-number">19.7.3.1. </span>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Comment"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">Comment</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Comment" title="Permalink to this definition">¶</a></dt> <dd><p>Comment element factory. This factory function creates a special element that will be serialized as an XML comment by the standard serializer. The comment string can be either a bytestring or a Unicode string. <em>text</em> is a string containing the comment string. Returns an element instance representing a comment.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.dump"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">elem</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.dump" title="Permalink to this definition">¶</a></dt> <dd><p>Writes an element tree or element structure to sys.stdout. This function should be used for debugging only.</p> <p>The exact output format is implementation dependent. In this version, it’s written as an ordinary XML file.</p> <p><em>elem</em> is an element tree or an individual element.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.fromstring"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">fromstring</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.fromstring" title="Permalink to this definition">¶</a></dt> <dd><p>Parses an XML section from a string constant. Same as <a class="reference internal" href="#xml.etree.ElementTree.XML" title="xml.etree.ElementTree.XML"><code class="xref py py-func docutils literal notranslate"><span class="pre">XML()</span></code></a>. <em>text</em> is a string containing XML data. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.fromstringlist"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">fromstringlist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.fromstringlist" title="Permalink to this definition">¶</a></dt> <dd><p>Parses an XML document from a sequence of string fragments. <em>sequence</em> is a list or other sequence containing XML data fragments. <em>parser</em> is an optional parser instance. If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> parser is used. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.iselement"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">iselement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.iselement" title="Permalink to this definition">¶</a></dt> <dd><p>Checks if an object appears to be a valid element object. <em>element</em> is an element instance. Returns a true value if this is an element object.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.iterparse"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">iterparse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">events</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.iterparse" title="Permalink to this definition">¶</a></dt> <dd><p>Parses an XML section into an element tree incrementally, and reports what’s going on to the user. <em>source</em> is a filename or file object containing XML data. <em>events</em> is a list of events to report back. If omitted, only “end” events are reported. <em>parser</em> is an optional parser instance. If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> parser is used. <em>parser</em> is not supported by <code class="docutils literal notranslate"><span class="pre">cElementTree</span></code>. Returns an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> providing <code class="docutils literal notranslate"><span class="pre">(event,</span> <span class="pre">elem)</span></code> pairs.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p><a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><code class="xref py py-func docutils literal notranslate"><span class="pre">iterparse()</span></code></a> only guarantees that it has seen the “>” character of a starting tag when it emits a “start” event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present.</p> <p>If you need a fully populated element, look for “end” events instead.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.parse"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.parse" title="Permalink to this definition">¶</a></dt> <dd><p>Parses an XML section into an element tree. <em>source</em> is a filename or file object containing XML data. <em>parser</em> is an optional parser instance. If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> parser is used. Returns an <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> instance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ProcessingInstruction"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">ProcessingInstruction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ProcessingInstruction" title="Permalink to this definition">¶</a></dt> <dd><p>PI element factory. This factory function creates a special element that will be serialized as an XML processing instruction. <em>target</em> is a string containing the PI target. <em>text</em> is a string containing the PI contents, if given. Returns an element instance, representing a processing instruction.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.register_namespace"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">register_namespace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.register_namespace" title="Permalink to this definition">¶</a></dt> <dd><p>Registers a namespace prefix. The registry is global, and any existing mapping for either the given prefix or the namespace URI will be removed. <em>prefix</em> is a namespace prefix. <em>uri</em> is a namespace uri. Tags and attributes in this namespace will be serialized with the given prefix, if at all possible.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.SubElement"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">SubElement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parent</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrib</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">extra</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.SubElement" title="Permalink to this definition">¶</a></dt> <dd><p>Subelement factory. This function creates an element instance, and appends it to an existing element.</p> <p>The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. <em>parent</em> is the parent element. <em>tag</em> is the subelement name. <em>attrib</em> is an optional dictionary, containing element attributes. <em>extra</em> contains additional attributes, given as keyword arguments. Returns an element instance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.tostring"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">tostring</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'xml'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostring" title="Permalink to this definition">¶</a></dt> <dd><p>Generates a string representation of an XML element, including all subelements. <em>element</em> is an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance. <em>encoding</em> <a class="footnote-reference brackets" href="#id5" id="id1">1</a> is the output encoding (default is US-ASCII). <em>method</em> is either <code class="docutils literal notranslate"><span class="pre">"xml"</span></code>, <code class="docutils literal notranslate"><span class="pre">"html"</span></code> or <code class="docutils literal notranslate"><span class="pre">"text"</span></code> (default is <code class="docutils literal notranslate"><span class="pre">"xml"</span></code>). Returns an encoded string containing the XML data.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.tostringlist"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">tostringlist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'xml'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostringlist" title="Permalink to this definition">¶</a></dt> <dd><p>Generates a string representation of an XML element, including all subelements. <em>element</em> is an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance. <em>encoding</em> <a class="footnote-reference brackets" href="#id5" id="id2">1</a> is the output encoding (default is US-ASCII). <em>method</em> is either <code class="docutils literal notranslate"><span class="pre">"xml"</span></code>, <code class="docutils literal notranslate"><span class="pre">"html"</span></code> or <code class="docutils literal notranslate"><span class="pre">"text"</span></code> (default is <code class="docutils literal notranslate"><span class="pre">"xml"</span></code>). Returns a list of encoded strings containing the XML data. It does not guarantee any specific sequence, except that <code class="docutils literal notranslate"><span class="pre">"".join(tostringlist(element))</span> <span class="pre">==</span> <span class="pre">tostring(element)</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.XML"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XML</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XML" title="Permalink to this definition">¶</a></dt> <dd><p>Parses an XML section from a string constant. This function can be used to embed “XML literals” in Python code. <em>text</em> is a string containing XML data. <em>parser</em> is an optional parser instance. If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> parser is used. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLID"> <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XMLID</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLID" title="Permalink to this definition">¶</a></dt> <dd><p>Parses an XML section from a string constant, and also returns a dictionary which maps from element id:s to elements. <em>text</em> is a string containing XML data. <em>parser</em> is an optional parser instance. If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> parser is used. Returns a tuple containing an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance and a dictionary.</p> </dd></dl> </section> <section id="element-objects"> <span id="elementtree-element-objects"></span><h3><span class="section-number">19.7.3.2. </span>Element Objects<a class="headerlink" href="#element-objects" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">Element</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrib</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">extra</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element" title="Permalink to this definition">¶</a></dt> <dd><p>Element class. This class defines the Element interface, and provides a reference implementation of this interface.</p> <p>The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. <em>tag</em> is the element name. <em>attrib</em> is an optional dictionary, containing element attributes. <em>extra</em> contains additional attributes, given as keyword arguments.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.tag"> <span class="sig-name descname"><span class="pre">tag</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.tag" title="Permalink to this definition">¶</a></dt> <dd><p>A string identifying what kind of data this element represents (the element type, in other words).</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.text"> <span class="sig-name descname"><span class="pre">text</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.text" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.tail"> <span class="sig-name descname"><span class="pre">tail</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.tail" title="Permalink to this definition">¶</a></dt> <dd><p>These attributes can be used to hold additional data associated with the element. Their values are usually strings but may be any application-specific object. If the element is created from an XML file, the <em>text</em> attribute holds either the text between the element’s start tag and its first child or end tag, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, and the <em>tail</em> attribute holds either the text between the element’s end tag and the next tag, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. For the XML data</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt"><a><b></span>1<span class="nt"><c></span>2<span class="nt"><d/></span>3<span class="nt"></c></b></span>4<span class="nt"></a></span> </pre></div> </div> <p>the <em>a</em> element has <code class="docutils literal notranslate"><span class="pre">None</span></code> for both <em>text</em> and <em>tail</em> attributes, the <em>b</em> element has <em>text</em> <code class="docutils literal notranslate"><span class="pre">"1"</span></code> and <em>tail</em> <code class="docutils literal notranslate"><span class="pre">"4"</span></code>, the <em>c</em> element has <em>text</em> <code class="docutils literal notranslate"><span class="pre">"2"</span></code> and <em>tail</em> <code class="docutils literal notranslate"><span class="pre">None</span></code>, and the <em>d</em> element has <em>text</em> <code class="docutils literal notranslate"><span class="pre">None</span></code> and <em>tail</em> <code class="docutils literal notranslate"><span class="pre">"3"</span></code>.</p> <p>To collect the inner text of an element, see <a class="reference internal" href="#xml.etree.ElementTree.Element.itertext" title="xml.etree.ElementTree.Element.itertext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">itertext()</span></code></a>, for example <code class="docutils literal notranslate"><span class="pre">"".join(element.itertext())</span></code>.</p> <p>Applications may store arbitrary objects in these attributes.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.attrib"> <span class="sig-name descname"><span class="pre">attrib</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.attrib" title="Permalink to this definition">¶</a></dt> <dd><p>A dictionary containing the element’s attributes. Note that while the <em>attrib</em> value is always a real mutable Python dictionary, an ElementTree implementation may choose to use another internal representation, and create the dictionary only if someone asks for it. To take advantage of such implementations, use the dictionary methods below whenever possible.</p> </dd></dl> <p>The following dictionary-like methods work on the element attributes.</p> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.clear"> <span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.clear" title="Permalink to this definition">¶</a></dt> <dd><p>Resets an element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.get"> <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.get" title="Permalink to this definition">¶</a></dt> <dd><p>Gets the element attribute named <em>key</em>.</p> <p>Returns the attribute value, or <em>default</em> if the attribute was not found.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.items"> <span class="sig-name descname"><span class="pre">items</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.items" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the element attributes as a sequence of (name, value) pairs. The attributes are returned in an arbitrary order.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.keys"> <span class="sig-name descname"><span class="pre">keys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.keys" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the elements attribute names as a list. The names are returned in an arbitrary order.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.set"> <span class="sig-name descname"><span class="pre">set</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.set" title="Permalink to this definition">¶</a></dt> <dd><p>Set the attribute <em>key</em> on the element to <em>value</em>.</p> </dd></dl> <p>The following methods work on the element’s children (subelements).</p> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.append"> <span class="sig-name descname"><span class="pre">append</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subelement</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.append" title="Permalink to this definition">¶</a></dt> <dd><p>Adds the element <em>subelement</em> to the end of this elements internal list of subelements.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.extend"> <span class="sig-name descname"><span class="pre">extend</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subelements</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.extend" title="Permalink to this definition">¶</a></dt> <dd><p>Appends <em>subelements</em> from a sequence object with zero or more elements. Raises <code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code> if a subelement is not a valid object.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.find"> <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.find" title="Permalink to this definition">¶</a></dt> <dd><p>Finds the first subelement matching <em>match</em>. <em>match</em> may be a tag name or path. Returns an element instance or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.findall"> <span class="sig-name descname"><span class="pre">findall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findall" title="Permalink to this definition">¶</a></dt> <dd><p>Finds all matching subelements, by tag name or path. Returns a list containing all matching elements in document order.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.findtext"> <span class="sig-name descname"><span class="pre">findtext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findtext" title="Permalink to this definition">¶</a></dt> <dd><p>Finds text for the first subelement matching <em>match</em>. <em>match</em> may be a tag name or path. Returns the text content of the first matching element, or <em>default</em> if no element was found. Note that if the matching element has no text content an empty string is returned.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.getchildren"> <span class="sig-name descname"><span class="pre">getchildren</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.getchildren" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 2.7: </span>Use <code class="docutils literal notranslate"><span class="pre">list(elem)</span></code> or iteration.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.getiterator"> <span class="sig-name descname"><span class="pre">getiterator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.getiterator" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 2.7: </span>Use method <a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.iter()</span></code></a> instead.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.insert"> <span class="sig-name descname"><span class="pre">insert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">index</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">element</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.insert" title="Permalink to this definition">¶</a></dt> <dd><p>Inserts a subelement at the given position in this element.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.iter"> <span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iter" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a tree <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. If <em>tag</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> or <code class="docutils literal notranslate"><span class="pre">'*'</span></code>, only elements whose tag equals <em>tag</em> are returned from the iterator. If the tree structure is modified during iteration, the result is undefined.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.iterfind"> <span class="sig-name descname"><span class="pre">iterfind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iterfind" title="Permalink to this definition">¶</a></dt> <dd><p>Finds all matching subelements, by tag name or path. Returns an iterable yielding all matching elements in document order.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.itertext"> <span class="sig-name descname"><span class="pre">itertext</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.itertext" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a text iterator. The iterator loops over this element and all subelements, in document order, and returns all inner text.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.makeelement"> <span class="sig-name descname"><span class="pre">makeelement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrib</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.makeelement" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a new element object of the same type as this element. Do not call this method, use the <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal notranslate"><span class="pre">SubElement()</span></code></a> factory function instead.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.remove"> <span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subelement</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.remove" title="Permalink to this definition">¶</a></dt> <dd><p>Removes <em>subelement</em> from the element. Unlike the find* methods this method compares elements based on the instance identity, not on tag value or contents.</p> </dd></dl> <p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> objects also support the following sequence type methods for working with subelements: <a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a>.</p> <p>Caution: Elements with no subelements will test as <code class="docutils literal notranslate"><span class="pre">False</span></code>. This behavior will change in future versions. Use specific <code class="docutils literal notranslate"><span class="pre">len(elem)</span></code> or <code class="docutils literal notranslate"><span class="pre">elem</span> <span class="pre">is</span> <span class="pre">None</span></code> test instead.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span> <span class="c1"># careful!</span> <span class="nb">print</span> <span class="s2">"element not found, or element has no subelements"</span> <span class="k">if</span> <span class="n">element</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="nb">print</span> <span class="s2">"element not found"</span> </pre></div> </div> </dd></dl> </section> <section id="elementtree-objects"> <span id="elementtree-elementtree-objects"></span><h3><span class="section-number">19.7.3.3. </span>ElementTree Objects<a class="headerlink" href="#elementtree-objects" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">ElementTree</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree" title="Permalink to this definition">¶</a></dt> <dd><p>ElementTree wrapper class. This class represents an entire element hierarchy, and adds some extra support for serialization to and from standard XML.</p> <p><em>element</em> is the root element. The tree is initialized with the contents of the XML <em>file</em> if given.</p> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree._setroot"> <span class="sig-name descname"><span class="pre">_setroot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree._setroot" title="Permalink to this definition">¶</a></dt> <dd><p>Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. <em>element</em> is an element instance.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.find"> <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.find" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.find()</span></code></a>, starting at the root of the tree.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.findall"> <span class="sig-name descname"><span class="pre">findall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findall" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.findall()</span></code></a>, starting at the root of the tree.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.findtext"> <span class="sig-name descname"><span class="pre">findtext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findtext" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.findtext" title="xml.etree.ElementTree.Element.findtext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.findtext()</span></code></a>, starting at the root of the tree.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.getiterator"> <span class="sig-name descname"><span class="pre">getiterator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getiterator" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 2.7: </span>Use method <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.iter" title="xml.etree.ElementTree.ElementTree.iter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ElementTree.iter()</span></code></a> instead.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.getroot"> <span class="sig-name descname"><span class="pre">getroot</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getroot" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the root element for this tree.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.iter"> <span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iter" title="Permalink to this definition">¶</a></dt> <dd><p>Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. <em>tag</em> is the tag to look for (default is to return all elements).</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.iterfind"> <span class="sig-name descname"><span class="pre">iterfind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iterfind" title="Permalink to this definition">¶</a></dt> <dd><p>Finds all matching subelements, by tag name or path. Same as getroot().iterfind(match). Returns an iterable yielding all matching elements in document order.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.parse"> <span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.parse" title="Permalink to this definition">¶</a></dt> <dd><p>Loads an external XML section into this element tree. <em>source</em> is a file name or file object. <em>parser</em> is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.write"> <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">xml_declaration</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default_namespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'xml'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.write" title="Permalink to this definition">¶</a></dt> <dd><p>Writes the element tree to a file, as XML. <em>file</em> is a file name, or a file object opened for writing. <em>encoding</em> <a class="footnote-reference brackets" href="#id5" id="id3">1</a> is the output encoding (default is US-ASCII). <em>xml_declaration</em> controls if an XML declaration should be added to the file. Use <code class="docutils literal notranslate"><span class="pre">False</span></code> for never, <code class="docutils literal notranslate"><span class="pre">True</span></code> for always, <code class="docutils literal notranslate"><span class="pre">None</span></code> for only if not US-ASCII or UTF-8 (default is <code class="docutils literal notranslate"><span class="pre">None</span></code>). <em>default_namespace</em> sets the default XML namespace (for “xmlns”). <em>method</em> is either <code class="docutils literal notranslate"><span class="pre">"xml"</span></code>, <code class="docutils literal notranslate"><span class="pre">"html"</span></code> or <code class="docutils literal notranslate"><span class="pre">"text"</span></code> (default is <code class="docutils literal notranslate"><span class="pre">"xml"</span></code>). Returns an encoded string.</p> </dd></dl> </dd></dl> <p>This is the XML file that is going to be manipulated:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">html</span><span class="o">></span> <span class="o"><</span><span class="n">head</span><span class="o">></span> <span class="o"><</span><span class="n">title</span><span class="o">></span><span class="n">Example</span> <span class="n">page</span><span class="o"></</span><span class="n">title</span><span class="o">></span> <span class="o"></</span><span class="n">head</span><span class="o">></span> <span class="o"><</span><span class="n">body</span><span class="o">></span> <span class="o"><</span><span class="n">p</span><span class="o">></span><span class="n">Moved</span> <span class="n">to</span> <span class="o"><</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">"http://example.org/"</span><span class="o">></span><span class="n">example</span><span class="o">.</span><span class="n">org</span><span class="o"></</span><span class="n">a</span><span class="o">></span> <span class="ow">or</span> <span class="o"><</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">"http://example.com/"</span><span class="o">></span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="o"></</span><span class="n">a</span><span class="o">>.</</span><span class="n">p</span><span class="o">></span> <span class="o"></</span><span class="n">body</span><span class="o">></span> <span class="o"></</span><span class="n">html</span><span class="o">></span> </pre></div> </div> <p>Example of changing the attribute “target” of every link in first paragraph:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="kn">import</span> <span class="n">ElementTree</span> <span class="gp">>>> </span><span class="n">tree</span> <span class="o">=</span> <span class="n">ElementTree</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">"index.xhtml"</span><span class="p">)</span> <span class="go"><Element 'html' at 0xb77e6fac></span> <span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">"body/p"</span><span class="p">)</span> <span class="c1"># Finds first occurrence of tag p in body</span> <span class="gp">>>> </span><span class="n">p</span> <span class="go"><Element 'p' at 0xb77ec26c></span> <span class="gp">>>> </span><span class="n">links</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s2">"a"</span><span class="p">))</span> <span class="c1"># Returns list of all links</span> <span class="gp">>>> </span><span class="n">links</span> <span class="go">[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span> <span class="c1"># Iterates through all found links</span> <span class="gp">... </span> <span class="n">i</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s2">"target"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"blank"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"output.xhtml"</span><span class="p">)</span> </pre></div> </div> </section> <section id="qname-objects"> <span id="elementtree-qname-objects"></span><h3><span class="section-number">19.7.3.4. </span>QName Objects<a class="headerlink" href="#qname-objects" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="xml.etree.ElementTree.QName"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">QName</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text_or_uri</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.QName" title="Permalink to this definition">¶</a></dt> <dd><p>QName wrapper. This can be used to wrap a QName attribute value, in order to get proper namespace handling on output. <em>text_or_uri</em> is a string containing the QName value, in the form {uri}local, or, if the tag argument is given, the URI part of a QName. If <em>tag</em> is given, the first argument is interpreted as a URI, and this argument is interpreted as a local name. <a class="reference internal" href="#xml.etree.ElementTree.QName" title="xml.etree.ElementTree.QName"><code class="xref py py-class docutils literal notranslate"><span class="pre">QName</span></code></a> instances are opaque.</p> </dd></dl> </section> <section id="treebuilder-objects"> <span id="elementtree-treebuilder-objects"></span><h3><span class="section-number">19.7.3.5. </span>TreeBuilder Objects<a class="headerlink" href="#treebuilder-objects" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">TreeBuilder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder" title="Permalink to this definition">¶</a></dt> <dd><p>Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. The <em>element_factory</em> is called to create new <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instances when given.</p> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.close"> <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.close" title="Permalink to this definition">¶</a></dt> <dd><p>Flushes the builder buffers, and returns the toplevel document element. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.data"> <span class="sig-name descname"><span class="pre">data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.data" title="Permalink to this definition">¶</a></dt> <dd><p>Adds text to the current element. <em>data</em> is a string. This should be either a bytestring, or a Unicode string.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.end"> <span class="sig-name descname"><span class="pre">end</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.end" title="Permalink to this definition">¶</a></dt> <dd><p>Closes the current element. <em>tag</em> is the element name. Returns the closed element.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.start"> <span class="sig-name descname"><span class="pre">start</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.start" title="Permalink to this definition">¶</a></dt> <dd><p>Opens a new element. <em>tag</em> is the element name. <em>attrs</em> is a dictionary containing element attributes. Returns the opened element.</p> </dd></dl> <p>In addition, a custom <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">TreeBuilder</span></code></a> object can provide the following method:</p> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.doctype"> <span class="sig-name descname"><span class="pre">doctype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pubid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">system</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="Permalink to this definition">¶</a></dt> <dd><p>Handles a doctype declaration. <em>name</em> is the doctype name. <em>pubid</em> is the public identifier. <em>system</em> is the system identifier. This method does not exist on the default <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">TreeBuilder</span></code></a> class.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.7.</span></p> </div> </dd></dl> </dd></dl> </section> <section id="xmlparser-objects"> <span id="elementtree-xmlparser-objects"></span><h3><span class="section-number">19.7.3.6. </span>XMLParser Objects<a class="headerlink" href="#xmlparser-objects" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XMLParser</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">html</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> structure builder for XML source data, based on the expat parser. <em>html</em> are predefined HTML entities. This flag is not supported by the current implementation. <em>target</em> is the target object. If omitted, the builder uses an instance of the standard TreeBuilder class. <em>encoding</em> <a class="footnote-reference brackets" href="#id5" id="id4">1</a> is optional. If given, the value overrides the encoding specified in the XML file.</p> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser.close"> <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.close" title="Permalink to this definition">¶</a></dt> <dd><p>Finishes feeding data to the parser. Returns an element structure.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser.doctype"> <span class="sig-name descname"><span class="pre">doctype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pubid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">system</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.doctype" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 2.7: </span>Define the <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TreeBuilder.doctype()</span></code></a> method on a custom TreeBuilder target.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser.feed"> <span class="sig-name descname"><span class="pre">feed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.feed" title="Permalink to this definition">¶</a></dt> <dd><p>Feeds data to the parser. <em>data</em> is encoded data.</p> </dd></dl> </dd></dl> <p><a class="reference internal" href="#xml.etree.ElementTree.XMLParser.feed" title="xml.etree.ElementTree.XMLParser.feed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">XMLParser.feed()</span></code></a> calls <em>target</em>'s <code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code> method for each opening tag, its <code class="xref py py-meth docutils literal notranslate"><span class="pre">end()</span></code> method for each closing tag, and data is processed by method <code class="xref py py-meth docutils literal notranslate"><span class="pre">data()</span></code>. <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.close" title="xml.etree.ElementTree.XMLParser.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">XMLParser.close()</span></code></a> calls <em>target</em>'s method <code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code>. <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> can be used not only for building a tree structure. This is an example of counting the maximum depth of an XML file:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="kn">import</span> <span class="n">XMLParser</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">MaxDepth</span><span class="p">:</span> <span class="c1"># The target object of the parser</span> <span class="gp">... </span> <span class="n">maxDepth</span> <span class="o">=</span> <span class="mi">0</span> <span class="gp">... </span> <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">attrib</span><span class="p">):</span> <span class="c1"># Called for each opening tag.</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">+=</span> <span class="mi">1</span> <span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">></span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span><span class="p">:</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">):</span> <span class="c1"># Called for each closing tag.</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">-=</span> <span class="mi">1</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="c1"># We do not need to do anything with data.</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c1"># Called when all data has been parsed.</span> <span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">target</span> <span class="o">=</span> <span class="n">MaxDepth</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">XMLParser</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">target</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">exampleXml</span> <span class="o">=</span> <span class="s2">"""</span> <span class="gp">... </span><span class="s2"><a></span> <span class="gp">... </span><span class="s2"> <b></span> <span class="gp">... </span><span class="s2"> </b></span> <span class="gp">... </span><span class="s2"> <b></span> <span class="gp">... </span><span class="s2"> <c></span> <span class="gp">... </span><span class="s2"> <d></span> <span class="gp">... </span><span class="s2"> </d></span> <span class="gp">... </span><span class="s2"> </c></span> <span class="gp">... </span><span class="s2"> </b></span> <span class="gp">... </span><span class="s2"></a>"""</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">exampleXml</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="go">4</span> </pre></div> </div> <p class="rubric">Footnotes</p> <dl class="footnote brackets"> <dt class="label" id="id5"><span class="brackets">1</span><span class="fn-backref">(<a href="#id1">1</a>,<a href="#id2">2</a>,<a href="#id3">3</a>,<a href="#id4">4</a>)</span></dt> <dd><p>The encoding string included in XML output should conform to the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is not. See <a class="reference external" href="https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl">https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl</a> and <a class="reference external" href="https://www.iana.org/assignments/character-sets/character-sets.xhtml">https://www.iana.org/assignments/character-sets/character-sets.xhtml</a>.</p> </dd> </dl> </section> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">19.7. <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a><ul> <li><a class="reference internal" href="#tutorial">19.7.1. Tutorial</a><ul> <li><a class="reference internal" href="#xml-tree-and-elements">19.7.1.1. XML tree and elements</a></li> <li><a class="reference internal" href="#parsing-xml">19.7.1.2. Parsing XML</a></li> <li><a class="reference internal" href="#finding-interesting-elements">19.7.1.3. Finding interesting elements</a></li> <li><a class="reference internal" href="#modifying-an-xml-file">19.7.1.4. Modifying an XML File</a></li> <li><a class="reference internal" href="#building-xml-documents">19.7.1.5. Building XML documents</a></li> <li><a class="reference internal" href="#parsing-xml-with-namespaces">19.7.1.6. Parsing XML with Namespaces</a></li> <li><a class="reference internal" href="#additional-resources">19.7.1.7. Additional resources</a></li> </ul> </li> <li><a class="reference internal" href="#xpath-support">19.7.2. XPath support</a><ul> <li><a class="reference internal" href="#example">19.7.2.1. Example</a></li> <li><a class="reference internal" href="#supported-xpath-syntax">19.7.2.2. Supported XPath syntax</a></li> </ul> </li> <li><a class="reference internal" href="#reference">19.7.3. Reference</a><ul> <li><a class="reference internal" href="#functions">19.7.3.1. Functions</a></li> <li><a class="reference internal" href="#element-objects">19.7.3.2. Element Objects</a></li> <li><a class="reference internal" href="#elementtree-objects">19.7.3.3. ElementTree Objects</a></li> <li><a class="reference internal" href="#qname-objects">19.7.3.4. QName Objects</a></li> <li><a class="reference internal" href="#treebuilder-objects">19.7.3.5. TreeBuilder Objects</a></li> <li><a class="reference internal" href="#xmlparser-objects">19.7.3.6. XMLParser Objects</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="xml.html" title="previous chapter"><span class="section-number">19.5. </span>XML Processing Modules</a></p> <h4>Next topic</h4> <p class="topless"><a href="xml.dom.html" title="next chapter"><span class="section-number">19.8. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom</span></code> — The Document Object Model API</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/xml.etree.elementtree.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> <input type="submit" value="Go" /> </form> </div> </div> <script>$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="xml.dom.html" title="19.8. xml.dom — The Document Object Model API" >next</a> |</li> <li class="right" > <a href="xml.html" title="19.5. XML Processing Modules" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="markup.html" ><span class="section-number">19. </span>Structured Markup Processing Tools</a> »</li> <li class="nav-item nav-item-this"><a href=""><span class="section-number">19.7. </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a></li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on December 09, 2024. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 4.3.2. </div> </body> </html>
Close