/
usr
/
share
/
doc
/
python2-docs
/
html
/
library
/
/usr/share/doc/python2-docs/html/library
mkdir
upload
Name
Size
Mode
Actions
2to3.html
59507
0644
edit
dl
rm
abc.html
25826
0644
edit
dl
rm
aepack.html
14164
0644
edit
dl
rm
aetools.html
16389
0644
edit
dl
rm
aetypes.html
21021
0644
edit
dl
rm
aifc.html
25064
0644
edit
dl
rm
al.html
18672
0644
edit
dl
rm
allos.html
35297
0644
edit
dl
rm
anydbm.html
18124
0644
edit
dl
rm
archiving.html
10078
0644
edit
dl
rm
argparse.html
263616
0644
edit
dl
rm
array.html
31855
0644
edit
dl
rm
ast.html
38167
0644
edit
dl
rm
asynchat.html
33930
0644
edit
dl
rm
asyncore.html
40631
0644
edit
dl
rm
atexit.html
18455
0644
edit
dl
rm
audioop.html
34252
0644
edit
dl
rm
autogil.html
8733
0644
edit
dl
rm
base64.html
21862
0644
edit
dl
rm
basehttpserver.html
37441
0644
edit
dl
rm
bastion.html
11798
0644
edit
dl
rm
bdb.html
41395
0644
edit
dl
rm
binascii.html
22997
0644
edit
dl
rm
binhex.html
11309
0644
edit
dl
rm
bisect.html
24666
0644
edit
dl
rm
bsddb.html
28589
0644
edit
dl
rm
bz2.html
29048
0644
edit
dl
rm
calendar.html
41762
0644
edit
dl
rm
carbon.html
51998
0644
edit
dl
rm
cd.html
30033
0644
edit
dl
rm
cgi.html
55922
0644
edit
dl
rm
cgihttpserver.html
14026
0644
edit
dl
rm
cgitb.html
12263
0644
edit
dl
rm
chunk.html
15899
0644
edit
dl
rm
cmath.html
28365
0644
edit
dl
rm
cmd.html
29042
0644
edit
dl
rm
code.html
26959
0644
edit
dl
rm
codecs.html
118258
0644
edit
dl
rm
codeop.html
15898
0644
edit
dl
rm
collections.html
147647
0644
edit
dl
rm
colorpicker.html
8035
0644
edit
dl
rm
colorsys.html
11919
0644
edit
dl
rm
commands.html
15516
0644
edit
dl
rm
compileall.html
18621
0644
edit
dl
rm
compiler.html
75897
0644
edit
dl
rm
configparser.html
67751
0644
edit
dl
rm
constants.html
13979
0644
edit
dl
rm
contextlib.html
22777
0644
edit
dl
rm
cookie.html
41854
0644
edit
dl
rm
cookielib.html
91032
0644
edit
dl
rm
copy.html
13004
0644
edit
dl
rm
copy_reg.html
14691
0644
edit
dl
rm
crypt.html
10647
0644
edit
dl
rm
crypto.html
7761
0644
edit
dl
rm
csv.html
76456
0644
edit
dl
rm
ctypes.html
264575
0644
edit
dl
rm
curses.ascii.html
24873
0644
edit
dl
rm
curses.html
167501
0644
edit
dl
rm
curses.panel.html
15824
0644
edit
dl
rm
custominterp.html
8026
0644
edit
dl
rm
datatypes.html
18015
0644
edit
dl
rm
datetime.html
253438
0644
edit
dl
rm
dbhash.html
16825
0644
edit
dl
rm
dbm.html
13507
0644
edit
dl
rm
debug.html
10703
0644
edit
dl
rm
decimal.html
222315
0644
edit
dl
rm
development.html
14828
0644
edit
dl
rm
difflib.html
91975
0644
edit
dl
rm
dircache.html
12299
0644
edit
dl
rm
dis.html
84156
0644
edit
dl
rm
distribution.html
7679
0644
edit
dl
rm
distutils.html
10152
0644
edit
dl
rm
dl.html
17668
0644
edit
dl
rm
doctest.html
185883
0644
edit
dl
rm
docxmlrpcserver.html
17573
0644
edit
dl
rm
dumbdbm.html
15549
0644
edit
dl
rm
dummy_thread.html
10065
0644
edit
dl
rm
dummy_threading.html
8934
0644
edit
dl
rm
easydialogs.html
33124
0644
edit
dl
rm
email-examples.html
47424
0644
edit
dl
rm
email.charset.html
29424
0644
edit
dl
rm
email.encoders.html
12847
0644
edit
dl
rm
email.errors.html
17952
0644
edit
dl
rm
email.generator.html
23141
0644
edit
dl
rm
email.header.html
29108
0644
edit
dl
rm
email.html
55722
0644
edit
dl
rm
email.iterators.html
12623
0644
edit
dl
rm
email.message.html
70189
0644
edit
dl
rm
email.mime.html
31655
0644
edit
dl
rm
email.parser.html
35142
0644
edit
dl
rm
email.utils.html
27076
0644
edit
dl
rm
ensurepip.html
18327
0644
edit
dl
rm
errno.html
40246
0644
edit
dl
rm
exceptions.html
63693
0644
edit
dl
rm
fcntl.html
26245
0644
edit
dl
rm
filecmp.html
23997
0644
edit
dl
rm
fileformats.html
9653
0644
edit
dl
rm
fileinput.html
27071
0644
edit
dl
rm
filesys.html
10866
0644
edit
dl
rm
fl.html
56525
0644
edit
dl
rm
fm.html
13009
0644
edit
dl
rm
fnmatch.html
16258
0644
edit
dl
rm
formatter.html
37371
0644
edit
dl
rm
fpectl.html
16879
0644
edit
dl
rm
fpformat.html
11455
0644
edit
dl
rm
fractions.html
24888
0644
edit
dl
rm
framework.html
36934
0644
edit
dl
rm
frameworks.html
7551
0644
edit
dl
rm
ftplib.html
49577
0644
edit
dl
rm
functions.html
205640
0644
edit
dl
rm
functools.html
29645
0644
edit
dl
rm
future_builtins.html
14477
0644
edit
dl
rm
gc.html
28423
0644
edit
dl
rm
gdbm.html
17805
0644
edit
dl
rm
gensuitemodule.html
12574
0644
edit
dl
rm
getopt.html
25280
0644
edit
dl
rm
getpass.html
11426
0644
edit
dl
rm
gettext.html
84971
0644
edit
dl
rm
gl.html
24342
0644
edit
dl
rm
glob.html
14430
0644
edit
dl
rm
grp.html
11316
0644
edit
dl
rm
gzip.html
20576
0644
edit
dl
rm
hashlib.html
25467
0644
edit
dl
rm
heapq.html
34890
0644
edit
dl
rm
hmac.html
14374
0644
edit
dl
rm
hotshot.html
20147
0644
edit
dl
rm
htmllib.html
27682
0644
edit
dl
rm
htmlparser.html
42433
0644
edit
dl
rm
httplib.html
70932
0644
edit
dl
rm
i18n.html
10047
0644
edit
dl
rm
ic.html
18654
0644
edit
dl
rm
idle.html
42148
0644
edit
dl
rm
imageop.html
16099
0644
edit
dl
rm
imaplib.html
58515
0644
edit
dl
rm
imgfile.html
12732
0644
edit
dl
rm
imghdr.html
12238
0644
edit
dl
rm
imp.html
37603
0644
edit
dl
rm
importlib.html
8926
0644
edit
dl
rm
imputil.html
33563
0644
edit
dl
rm
index.html
79088
0644
edit
dl
rm
inspect.html
56823
0644
edit
dl
rm
internet.html
26138
0644
edit
dl
rm
intro.html
9353
0644
edit
dl
rm
io.html
113701
0644
edit
dl
rm
ipc.html
16598
0644
edit
dl
rm
itertools.html
125397
0644
edit
dl
rm
jpeg.html
13757
0644
edit
dl
rm
json.html
73678
0644
edit
dl
rm
keyword.html
8210
0644
edit
dl
rm
language.html
11687
0644
edit
dl
rm
linecache.html
11416
0644
edit
dl
rm
locale.html
61576
0644
edit
dl
rm
logging.config.html
80045
0644
edit
dl
rm
logging.handlers.html
80194
0644
edit
dl
rm
logging.html
110254
0644
edit
dl
rm
mac.html
23376
0644
edit
dl
rm
macos.html
16129
0644
edit
dl
rm
macosa.html
14080
0644
edit
dl
rm
macostools.html
16898
0644
edit
dl
rm
macpath.html
8386
0644
edit
dl
rm
mailbox.html
171121
0644
edit
dl
rm
mailcap.html
14132
0644
edit
dl
rm
markup.html
19862
0644
edit
dl
rm
marshal.html
19456
0644
edit
dl
rm
math.html
44015
0644
edit
dl
rm
md5.html
15123
0644
edit
dl
rm
mhlib.html
24123
0644
edit
dl
rm
mimetools.html
21206
0644
edit
dl
rm
mimetypes.html
30634
0644
edit
dl
rm
mimewriter.html
16078
0644
edit
dl
rm
mimify.html
15279
0644
edit
dl
rm
miniaeframe.html
13109
0644
edit
dl
rm
misc.html
7236
0644
edit
dl
rm
mm.html
9570
0644
edit
dl
rm
mmap.html
30832
0644
edit
dl
rm
modulefinder.html
18096
0644
edit
dl
rm
modules.html
9017
0644
edit
dl
rm
msilib.html
57853
0644
edit
dl
rm
msvcrt.html
21232
0644
edit
dl
rm
multifile.html
26256
0644
edit
dl
rm
multiprocessing.html
414535
0644
edit
dl
rm
mutex.html
12147
0644
edit
dl
rm
netdata.html
18379
0644
edit
dl
rm
netrc.html
14021
0644
edit
dl
rm
new.html
13177
0644
edit
dl
rm
nis.html
11502
0644
edit
dl
rm
nntplib.html
45689
0644
edit
dl
rm
numbers.html
40636
0644
edit
dl
rm
numeric.html
14292
0644
edit
dl
rm
operator.html
93622
0644
edit
dl
rm
optparse.html
250109
0644
edit
dl
rm
os.html
240195
0644
edit
dl
rm
os.path.html
43402
0644
edit
dl
rm
ossaudiodev.html
45594
0644
edit
dl
rm
othergui.html
9403
0644
edit
dl
rm
parser.html
42558
0644
edit
dl
rm
pdb.html
38603
0644
edit
dl
rm
persistence.html
15676
0644
edit
dl
rm
pickle.html
109684
0644
edit
dl
rm
pickletools.html
11475
0644
edit
dl
rm
pipes.html
19685
0644
edit
dl
rm
pkgutil.html
27367
0644
edit
dl
rm
platform.html
31599
0644
edit
dl
rm
plistlib.html
18344
0644
edit
dl
rm
popen2.html
27601
0644
edit
dl
rm
poplib.html
24320
0644
edit
dl
rm
posix.html
16626
0644
edit
dl
rm
posixfile.html
21387
0644
edit
dl
rm
pprint.html
32207
0644
edit
dl
rm
profile.html
72788
0644
edit
dl
rm
pty.html
10195
0644
edit
dl
rm
pwd.html
12388
0644
edit
dl
rm
pyclbr.html
15812
0644
edit
dl
rm
pydoc.html
13650
0644
edit
dl
rm
pyexpat.html
80886
0644
edit
dl
rm
python.html
12854
0644
edit
dl
rm
py_compile.html
11932
0644
edit
dl
rm
queue.html
26856
0644
edit
dl
rm
quopri.html
12739
0644
edit
dl
rm
random.html
42722
0644
edit
dl
rm
re.html
155985
0644
edit
dl
rm
readline.html
37383
0644
edit
dl
rm
repr.html
21778
0644
edit
dl
rm
resource.html
28286
0644
edit
dl
rm
restricted.html
12366
0644
edit
dl
rm
rexec.html
40566
0644
edit
dl
rm
rfc822.html
46408
0644
edit
dl
rm
rlcompleter.html
14460
0644
edit
dl
rm
robotparser.html
13403
0644
edit
dl
rm
runpy.html
21925
0644
edit
dl
rm
sched.html
19878
0644
edit
dl
rm
scrolledtext.html
9728
0644
edit
dl
rm
select.html
44090
0644
edit
dl
rm
sets.html
40151
0644
edit
dl
rm
sgi.html
10371
0644
edit
dl
rm
sgmllib.html
34464
0644
edit
dl
rm
sha.html
13062
0644
edit
dl
rm
shelve.html
29955
0644
edit
dl
rm
shlex.html
35033
0644
edit
dl
rm
shutil.html
45454
0644
edit
dl
rm
signal.html
34023
0644
edit
dl
rm
simplehttpserver.html
20351
0644
edit
dl
rm
simplexmlrpcserver.html
37794
0644
edit
dl
rm
site.html
26887
0644
edit
dl
rm
smtpd.html
13587
0644
edit
dl
rm
smtplib.html
47054
0644
edit
dl
rm
sndhdr.html
10993
0644
edit
dl
rm
socket.html
116414
0644
edit
dl
rm
socketserver.html
76430
0644
edit
dl
rm
someos.html
16472
0644
edit
dl
rm
spwd.html
11163
0644
edit
dl
rm
sqlite3.html
150014
0644
edit
dl
rm
ssl.html
202453
0644
edit
dl
rm
stat.html
34597
0644
edit
dl
rm
statvfs.html
11326
0644
edit
dl
rm
stdtypes.html
298144
0644
edit
dl
rm
string.html
120219
0644
edit
dl
rm
stringio.html
20061
0644
edit
dl
rm
stringprep.html
17729
0644
edit
dl
rm
strings.html
15905
0644
edit
dl
rm
struct.html
44896
0644
edit
dl
rm
subprocess.html
110447
0644
edit
dl
rm
sun.html
7253
0644
edit
dl
rm
sunau.html
30011
0644
edit
dl
rm
sunaudio.html
19236
0644
edit
dl
rm
symbol.html
8140
0644
edit
dl
rm
symtable.html
25871
0644
edit
dl
rm
sys.html
110996
0644
edit
dl
rm
sysconfig.html
26299
0644
edit
dl
rm
syslog.html
19737
0644
edit
dl
rm
tabnanny.html
11393
0644
edit
dl
rm
tarfile.html
88728
0644
edit
dl
rm
telnetlib.html
27782
0644
edit
dl
rm
tempfile.html
31903
0644
edit
dl
rm
termios.html
17309
0644
edit
dl
rm
test.html
57030
0644
edit
dl
rm
textwrap.html
30135
0644
edit
dl
rm
thread.html
21486
0644
edit
dl
rm
threading.html
86655
0644
edit
dl
rm
time.html
63798
0644
edit
dl
rm
timeit.html
40529
0644
edit
dl
rm
tix.html
50582
0644
edit
dl
rm
tk.html
26568
0644
edit
dl
rm
tkinter.html
84316
0644
edit
dl
rm
token.html
20999
0644
edit
dl
rm
tokenize.html
20607
0644
edit
dl
rm
trace.html
28668
0644
edit
dl
rm
traceback.html
40900
0644
edit
dl
rm
ttk.html
108515
0644
edit
dl
rm
tty.html
9753
0644
edit
dl
rm
turtle.html
230483
0644
edit
dl
rm
types.html
29833
0644
edit
dl
rm
undoc.html
24680
0644
edit
dl
rm
unicodedata.html
20186
0644
edit
dl
rm
unittest.html
225021
0644
edit
dl
rm
unix.html
11223
0644
edit
dl
rm
urllib.html
68266
0644
edit
dl
rm
urllib2.html
113380
0644
edit
dl
rm
urlparse.html
43474
0644
edit
dl
rm
user.html
12707
0644
edit
dl
rm
userdict.html
32069
0644
edit
dl
rm
uu.html
11810
0644
edit
dl
rm
uuid.html
30137
0644
edit
dl
rm
warnings.html
50950
0644
edit
dl
rm
wave.html
24894
0644
edit
dl
rm
weakref.html
38808
0644
edit
dl
rm
webbrowser.html
26565
0644
edit
dl
rm
whichdb.html
9499
0644
edit
dl
rm
windows.html
9817
0644
edit
dl
rm
winsound.html
20466
0644
edit
dl
rm
wsgiref.html
88384
0644
edit
dl
rm
xdrlib.html
33018
0644
edit
dl
rm
xml.dom.html
97877
0644
edit
dl
rm
xml.dom.minidom.html
43230
0644
edit
dl
rm
xml.dom.pulldom.html
13835
0644
edit
dl
rm
xml.etree.elementtree.html
111804
0644
edit
dl
rm
xml.html
18007
0644
edit
dl
rm
xml.sax.handler.html
41877
0644
edit
dl
rm
xml.sax.html
23458
0644
edit
dl
rm
xml.sax.reader.html
44802
0644
edit
dl
rm
xml.sax.utils.html
16087
0644
edit
dl
rm
xmlrpclib.html
69191
0644
edit
dl
rm
zipfile.html
62227
0644
edit
dl
rm
zipimport.html
22769
0644
edit
dl
rm
zlib.html
31230
0644
edit
dl
rm
_winreg.html
64631
0644
edit
dl
rm
__builtin__.html
11069
0644
edit
dl
rm
__future__.html
14688
0644
edit
dl
rm
__main__.html
7512
0644
edit
dl
rm
Edit:
/usr/share/doc/python2-docs/html/library/pyexpat.html
(80886B)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>19.15. xml.parsers.expat — Fast XML parsing using Expat — Python 2.7.16 documentation</title> <link rel="stylesheet" href="../_static/classic.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.16 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="20. Internet Protocols and Support" href="internet.html" /> <link rel="prev" title="19.14. xml.sax.xmlreader — Interface for XML parsers" href="xml.sax.reader.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/2/library/pyexpat.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <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="internet.html" title="20. Internet Protocols and Support" accesskey="N">next</a> |</li> <li class="right" > <a href="xml.sax.reader.html" title="19.14. xml.sax.xmlreader — Interface for XML parsers" 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.16 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">19. Structured Markup Processing Tools</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-xml.parsers.expat"> <span id="xml-parsers-expat-fast-xml-parsing-using-expat"></span><h1>19.15. <a class="reference internal" href="#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> — Fast XML parsing using Expat<a class="headerlink" href="#module-xml.parsers.expat" title="Permalink to this headline">¶</a></h1> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code> 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> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> <p id="index-0">The <a class="reference internal" href="#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> module is a Python interface to the Expat non-validating XML parser. The module provides a single extension type, <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code>, that represents the current state of an XML parser. After an <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> object has been created, various attributes of the object can be set to handler functions. When an XML document is then fed to the parser, the handler functions are called for the character data and markup in the XML document.</p> <p id="index-1">This module uses the <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code> module to provide access to the Expat parser. Direct use of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code> module is deprecated.</p> <p>This module provides one exception and one type object:</p> <dl class="exception"> <dt id="xml.parsers.expat.ExpatError"> <em class="property">exception </em><code class="descclassname">xml.parsers.expat.</code><code class="descname">ExpatError</code><a class="headerlink" href="#xml.parsers.expat.ExpatError" title="Permalink to this definition">¶</a></dt> <dd><p>The exception raised when Expat reports an error. See section <a class="reference internal" href="#expaterror-objects"><span class="std std-ref">ExpatError Exceptions</span></a> for more information on interpreting Expat errors.</p> </dd></dl> <dl class="exception"> <dt id="xml.parsers.expat.error"> <em class="property">exception </em><code class="descclassname">xml.parsers.expat.</code><code class="descname">error</code><a class="headerlink" href="#xml.parsers.expat.error" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for <a class="reference internal" href="#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExpatError</span></code></a>.</p> </dd></dl> <dl class="data"> <dt id="xml.parsers.expat.XMLParserType"> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XMLParserType</code><a class="headerlink" href="#xml.parsers.expat.XMLParserType" title="Permalink to this definition">¶</a></dt> <dd><p>The type of the return values from the <a class="reference internal" href="#xml.parsers.expat.ParserCreate" title="xml.parsers.expat.ParserCreate"><code class="xref py py-func docutils literal notranslate"><span class="pre">ParserCreate()</span></code></a> function.</p> </dd></dl> <p>The <a class="reference internal" href="#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> module contains two functions:</p> <dl class="function"> <dt id="xml.parsers.expat.ErrorString"> <code class="descclassname">xml.parsers.expat.</code><code class="descname">ErrorString</code><span class="sig-paren">(</span><em>errno</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.ErrorString" title="Permalink to this definition">¶</a></dt> <dd><p>Returns an explanatory string for a given error number <em>errno</em>.</p> </dd></dl> <dl class="function"> <dt id="xml.parsers.expat.ParserCreate"> <code class="descclassname">xml.parsers.expat.</code><code class="descname">ParserCreate</code><span class="sig-paren">(</span><span class="optional">[</span><em>encoding</em><span class="optional">[</span>, <em>namespace_separator</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.ParserCreate" title="Permalink to this definition">¶</a></dt> <dd><p>Creates and returns a new <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> object. <em>encoding</em>, if specified, must be a string naming the encoding used by the XML data. Expat doesn’t support as many encodings as Python does, and its repertoire of encodings can’t be extended; it supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If <em>encoding</em> <a class="footnote-reference" href="#id3" id="id1">[1]</a> is given it will override the implicit or explicit encoding of the document.</p> <p>Expat can optionally do XML namespace processing for you, enabled by providing a value for <em>namespace_separator</em>. The value must be a one-character string; a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised if the string has an illegal length (<code class="docutils literal notranslate"><span class="pre">None</span></code> is considered the same as omission). When namespace processing is enabled, element type names and attribute names that belong to a namespace will be expanded. The element name passed to the element handlers <code class="xref py py-attr docutils literal notranslate"><span class="pre">StartElementHandler</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">EndElementHandler</span></code> will be the concatenation of the namespace URI, the namespace separator character, and the local part of the name. If the namespace separator is a zero byte (<code class="docutils literal notranslate"><span class="pre">chr(0)</span></code>) then the namespace URI and the local part will be concatenated without any separator.</p> <p>For example, if <em>namespace_separator</em> is set to a space character (<code class="docutils literal notranslate"><span class="pre">'</span> <span class="pre">'</span></code>) and the following document is parsed:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span> <span class="nt"><root</span> <span class="na">xmlns =</span> <span class="s">"http://default-namespace.org/"</span> <span class="na">xmlns:py =</span> <span class="s">"http://www.python.org/ns/"</span><span class="nt">></span> <span class="nt"><py:elem1</span> <span class="nt">/></span> <span class="nt"><elem2</span> <span class="na">xmlns=</span><span class="s">""</span> <span class="nt">/></span> <span class="nt"></root></span> </pre></div> </div> <p><code class="xref py py-attr docutils literal notranslate"><span class="pre">StartElementHandler</span></code> will receive the following strings for each element:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">default</span><span class="o">-</span><span class="n">namespace</span><span class="o">.</span><span class="n">org</span><span class="o">/</span> <span class="n">root</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">python</span><span class="o">.</span><span class="n">org</span><span class="o">/</span><span class="n">ns</span><span class="o">/</span> <span class="n">elem1</span> <span class="n">elem2</span> </pre></div> </div> <p>Due to limitations in the <code class="docutils literal notranslate"><span class="pre">Expat</span></code> library used by <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code>, the <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> instance returned can only be used to parse a single XML document. Call <code class="docutils literal notranslate"><span class="pre">ParserCreate</span></code> for each document to provide unique parser instances.</p> </dd></dl> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference external" href="http://www.libexpat.org/">The Expat XML Parser</a></dt> <dd>Home page of the Expat project.</dd> </dl> </div> <div class="section" id="xmlparser-objects"> <span id="id2"></span><h2>19.15.1. XMLParser Objects<a class="headerlink" href="#xmlparser-objects" title="Permalink to this headline">¶</a></h2> <p><code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> objects have the following methods:</p> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.Parse"> <code class="descclassname">xmlparser.</code><code class="descname">Parse</code><span class="sig-paren">(</span><em>data</em><span class="optional">[</span>, <em>isfinal</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.Parse" title="Permalink to this definition">¶</a></dt> <dd><p>Parses the contents of the string <em>data</em>, calling the appropriate handler functions to process the parsed data. <em>isfinal</em> must be true on the final call to this method; it allows the parsing of a single file in fragments, not the submission of multiple files. <em>data</em> can be the empty string at any time.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.ParseFile"> <code class="descclassname">xmlparser.</code><code class="descname">ParseFile</code><span class="sig-paren">(</span><em>file</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.ParseFile" title="Permalink to this definition">¶</a></dt> <dd><p>Parse XML data reading from the object <em>file</em>. <em>file</em> only needs to provide the <code class="docutils literal notranslate"><span class="pre">read(nbytes)</span></code> method, returning the empty string when there’s no more data.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.SetBase"> <code class="descclassname">xmlparser.</code><code class="descname">SetBase</code><span class="sig-paren">(</span><em>base</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.SetBase" title="Permalink to this definition">¶</a></dt> <dd><p>Sets the base to be used for resolving relative URIs in system identifiers in declarations. Resolving relative identifiers is left to the application: this value will be passed through as the <em>base</em> argument to the <a class="reference internal" href="#xml.parsers.expat.xmlparser.ExternalEntityRefHandler" title="xml.parsers.expat.xmlparser.ExternalEntityRefHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">ExternalEntityRefHandler()</span></code></a>, <a class="reference internal" href="#xml.parsers.expat.xmlparser.NotationDeclHandler" title="xml.parsers.expat.xmlparser.NotationDeclHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">NotationDeclHandler()</span></code></a>, and <a class="reference internal" href="#xml.parsers.expat.xmlparser.UnparsedEntityDeclHandler" title="xml.parsers.expat.xmlparser.UnparsedEntityDeclHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">UnparsedEntityDeclHandler()</span></code></a> functions.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.GetBase"> <code class="descclassname">xmlparser.</code><code class="descname">GetBase</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.GetBase" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a string containing the base set by a previous call to <a class="reference internal" href="#xml.parsers.expat.xmlparser.SetBase" title="xml.parsers.expat.xmlparser.SetBase"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SetBase()</span></code></a>, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if <a class="reference internal" href="#xml.parsers.expat.xmlparser.SetBase" title="xml.parsers.expat.xmlparser.SetBase"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SetBase()</span></code></a> hasn’t been called.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.GetInputContext"> <code class="descclassname">xmlparser.</code><code class="descname">GetInputContext</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.GetInputContext" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the input data that generated the current event as a string. The data is in the encoding of the entity which contains the text. When called while an event handler is not active, the return value is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.ExternalEntityParserCreate"> <code class="descclassname">xmlparser.</code><code class="descname">ExternalEntityParserCreate</code><span class="sig-paren">(</span><em>context</em><span class="optional">[</span>, <em>encoding</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.ExternalEntityParserCreate" title="Permalink to this definition">¶</a></dt> <dd><p>Create a “child” parser which can be used to parse an external parsed entity referred to by content parsed by the parent parser. The <em>context</em> parameter should be the string passed to the <a class="reference internal" href="#xml.parsers.expat.xmlparser.ExternalEntityRefHandler" title="xml.parsers.expat.xmlparser.ExternalEntityRefHandler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExternalEntityRefHandler()</span></code></a> handler function, described below. The child parser is created with the <a class="reference internal" href="#xml.parsers.expat.xmlparser.ordered_attributes" title="xml.parsers.expat.xmlparser.ordered_attributes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordered_attributes</span></code></a>, <a class="reference internal" href="#xml.parsers.expat.xmlparser.returns_unicode" title="xml.parsers.expat.xmlparser.returns_unicode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returns_unicode</span></code></a> and <a class="reference internal" href="#xml.parsers.expat.xmlparser.specified_attributes" title="xml.parsers.expat.xmlparser.specified_attributes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">specified_attributes</span></code></a> set to the values of this parser.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.SetParamEntityParsing"> <code class="descclassname">xmlparser.</code><code class="descname">SetParamEntityParsing</code><span class="sig-paren">(</span><em>flag</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.SetParamEntityParsing" title="Permalink to this definition">¶</a></dt> <dd><p>Control parsing of parameter entities (including the external DTD subset). Possible <em>flag</em> values are <code class="xref py py-const docutils literal notranslate"><span class="pre">XML_PARAM_ENTITY_PARSING_NEVER</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">XML_PARAM_ENTITY_PARSING_ALWAYS</span></code>. Return true if setting the flag was successful.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.UseForeignDTD"> <code class="descclassname">xmlparser.</code><code class="descname">UseForeignDTD</code><span class="sig-paren">(</span><span class="optional">[</span><em>flag</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.UseForeignDTD" title="Permalink to this definition">¶</a></dt> <dd><p>Calling this with a true value for <em>flag</em> (the default) will cause Expat to call the <a class="reference internal" href="#xml.parsers.expat.xmlparser.ExternalEntityRefHandler" title="xml.parsers.expat.xmlparser.ExternalEntityRefHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ExternalEntityRefHandler</span></code></a> with <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> for all arguments to allow an alternate DTD to be loaded. If the document does not contain a document type declaration, the <a class="reference internal" href="#xml.parsers.expat.xmlparser.ExternalEntityRefHandler" title="xml.parsers.expat.xmlparser.ExternalEntityRefHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ExternalEntityRefHandler</span></code></a> will still be called, but the <a class="reference internal" href="#xml.parsers.expat.xmlparser.StartDoctypeDeclHandler" title="xml.parsers.expat.xmlparser.StartDoctypeDeclHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StartDoctypeDeclHandler</span></code></a> and <a class="reference internal" href="#xml.parsers.expat.xmlparser.EndDoctypeDeclHandler" title="xml.parsers.expat.xmlparser.EndDoctypeDeclHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EndDoctypeDeclHandler</span></code></a> will not be called.</p> <p>Passing a false value for <em>flag</em> will cancel a previous call that passed a true value, but otherwise has no effect.</p> <p>This method can only be called before the <a class="reference internal" href="#xml.parsers.expat.xmlparser.Parse" title="xml.parsers.expat.xmlparser.Parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Parse()</span></code></a> or <a class="reference internal" href="#xml.parsers.expat.xmlparser.ParseFile" title="xml.parsers.expat.xmlparser.ParseFile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ParseFile()</span></code></a> methods are called; calling it after either of those have been called causes <a class="reference internal" href="#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExpatError</span></code></a> to be raised with the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-attr docutils literal notranslate"><span class="pre">code</span></code></a> attribute set to <code class="xref py py-const docutils literal notranslate"><span class="pre">errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <p><code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> objects have the following attributes:</p> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.buffer_size"> <code class="descclassname">xmlparser.</code><code class="descname">buffer_size</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.buffer_size" title="Permalink to this definition">¶</a></dt> <dd><p>The size of the buffer used when <a class="reference internal" href="#xml.parsers.expat.xmlparser.buffer_text" title="xml.parsers.expat.xmlparser.buffer_text"><code class="xref py py-attr docutils literal notranslate"><span class="pre">buffer_text</span></code></a> is true. A new buffer size can be set by assigning a new integer value to this attribute. When the size is changed, the buffer will be flushed.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.6: </span>The buffer size can now be changed.</p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.buffer_text"> <code class="descclassname">xmlparser.</code><code class="descname">buffer_text</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.buffer_text" title="Permalink to this definition">¶</a></dt> <dd><p>Setting this to true causes the <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> object to buffer textual content returned by Expat to avoid multiple calls to the <a class="reference internal" href="#xml.parsers.expat.xmlparser.CharacterDataHandler" title="xml.parsers.expat.xmlparser.CharacterDataHandler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">CharacterDataHandler()</span></code></a> callback whenever possible. This can improve performance substantially since Expat normally breaks character data into chunks at every line ending. This attribute is false by default, and may be changed at any time.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.buffer_used"> <code class="descclassname">xmlparser.</code><code class="descname">buffer_used</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.buffer_used" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#xml.parsers.expat.xmlparser.buffer_text" title="xml.parsers.expat.xmlparser.buffer_text"><code class="xref py py-attr docutils literal notranslate"><span class="pre">buffer_text</span></code></a> is enabled, the number of bytes stored in the buffer. These bytes represent UTF-8 encoded text. This attribute has no meaningful interpretation when <a class="reference internal" href="#xml.parsers.expat.xmlparser.buffer_text" title="xml.parsers.expat.xmlparser.buffer_text"><code class="xref py py-attr docutils literal notranslate"><span class="pre">buffer_text</span></code></a> is false.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.ordered_attributes"> <code class="descclassname">xmlparser.</code><code class="descname">ordered_attributes</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.ordered_attributes" title="Permalink to this definition">¶</a></dt> <dd><p>Setting this attribute to a non-zero integer causes the attributes to be reported as a list rather than a dictionary. The attributes are presented in the order found in the document text. For each attribute, two list entries are presented: the attribute name and the attribute value. (Older versions of this module also used this format.) By default, this attribute is false; it may be changed at any time.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.returns_unicode"> <code class="descclassname">xmlparser.</code><code class="descname">returns_unicode</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.returns_unicode" title="Permalink to this definition">¶</a></dt> <dd><p>If this attribute is set to a non-zero integer, the handler functions will be passed Unicode strings. If <a class="reference internal" href="#xml.parsers.expat.xmlparser.returns_unicode" title="xml.parsers.expat.xmlparser.returns_unicode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returns_unicode</span></code></a> is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>, 8-bit strings containing UTF-8 encoded data will be passed to the handlers. This is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> by default when Python is built with Unicode support.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 1.6: </span>Can be changed at any time to affect the result type.</p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.specified_attributes"> <code class="descclassname">xmlparser.</code><code class="descname">specified_attributes</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.specified_attributes" title="Permalink to this definition">¶</a></dt> <dd><p>If set to a non-zero integer, the parser will report only those attributes which were specified in the document instance and not those which were derived from attribute declarations. Applications which set this need to be especially careful to use what additional information is available from the declarations as needed to comply with the standards for the behavior of XML processors. By default, this attribute is false; it may be changed at any time.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <p>The following attributes contain values relating to the most recent error encountered by an <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> object, and will only have correct values once a call to <code class="xref py py-meth docutils literal notranslate"><span class="pre">Parse()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">ParseFile()</span></code> has raised an <a class="reference internal" href="#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">xml.parsers.expat.ExpatError</span></code></a> exception.</p> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.ErrorByteIndex"> <code class="descclassname">xmlparser.</code><code class="descname">ErrorByteIndex</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.ErrorByteIndex" title="Permalink to this definition">¶</a></dt> <dd><p>Byte index at which an error occurred.</p> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.ErrorCode"> <code class="descclassname">xmlparser.</code><code class="descname">ErrorCode</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.ErrorCode" title="Permalink to this definition">¶</a></dt> <dd><p>Numeric code specifying the problem. This value can be passed to the <a class="reference internal" href="#xml.parsers.expat.ErrorString" title="xml.parsers.expat.ErrorString"><code class="xref py py-func docutils literal notranslate"><span class="pre">ErrorString()</span></code></a> function, or compared to one of the constants defined in the <code class="docutils literal notranslate"><span class="pre">errors</span></code> object.</p> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.ErrorColumnNumber"> <code class="descclassname">xmlparser.</code><code class="descname">ErrorColumnNumber</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.ErrorColumnNumber" title="Permalink to this definition">¶</a></dt> <dd><p>Column number at which an error occurred.</p> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.ErrorLineNumber"> <code class="descclassname">xmlparser.</code><code class="descname">ErrorLineNumber</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.ErrorLineNumber" title="Permalink to this definition">¶</a></dt> <dd><p>Line number at which an error occurred.</p> </dd></dl> <p>The following attributes contain values relating to the current parse location in an <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> object. During a callback reporting a parse event they indicate the location of the first of the sequence of characters that generated the event. When called outside of a callback, the position indicated will be just past the last parse event (regardless of whether there was an associated callback).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.4.</span></p> </div> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.CurrentByteIndex"> <code class="descclassname">xmlparser.</code><code class="descname">CurrentByteIndex</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.CurrentByteIndex" title="Permalink to this definition">¶</a></dt> <dd><p>Current byte index in the parser input.</p> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.CurrentColumnNumber"> <code class="descclassname">xmlparser.</code><code class="descname">CurrentColumnNumber</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.CurrentColumnNumber" title="Permalink to this definition">¶</a></dt> <dd><p>Current column number in the parser input.</p> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.xmlparser.CurrentLineNumber"> <code class="descclassname">xmlparser.</code><code class="descname">CurrentLineNumber</code><a class="headerlink" href="#xml.parsers.expat.xmlparser.CurrentLineNumber" title="Permalink to this definition">¶</a></dt> <dd><p>Current line number in the parser input.</p> </dd></dl> <p>Here is the list of handlers that can be set. To set a handler on an <code class="xref py py-class docutils literal notranslate"><span class="pre">xmlparser</span></code> object <em>o</em>, use <code class="docutils literal notranslate"><span class="pre">o.handlername</span> <span class="pre">=</span> <span class="pre">func</span></code>. <em>handlername</em> must be taken from the following list, and <em>func</em> must be a callable object accepting the correct number of arguments. The arguments are all strings, unless otherwise stated.</p> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.XmlDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">XmlDeclHandler</code><span class="sig-paren">(</span><em>version</em>, <em>encoding</em>, <em>standalone</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.XmlDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called when the XML declaration is parsed. The XML declaration is the (optional) declaration of the applicable version of the XML recommendation, the encoding of the document text, and an optional “standalone” declaration. <em>version</em> and <em>encoding</em> will be strings of the type dictated by the <a class="reference internal" href="#xml.parsers.expat.xmlparser.returns_unicode" title="xml.parsers.expat.xmlparser.returns_unicode"><code class="xref py py-attr docutils literal notranslate"><span class="pre">returns_unicode</span></code></a> attribute, and <em>standalone</em> will be <code class="docutils literal notranslate"><span class="pre">1</span></code> if the document is declared standalone, <code class="docutils literal notranslate"><span class="pre">0</span></code> if it is declared not to be standalone, or <code class="docutils literal notranslate"><span class="pre">-1</span></code> if the standalone clause was omitted. This is only available with Expat version 1.95.0 or newer.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.StartDoctypeDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">StartDoctypeDeclHandler</code><span class="sig-paren">(</span><em>doctypeName</em>, <em>systemId</em>, <em>publicId</em>, <em>has_internal_subset</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.StartDoctypeDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called when Expat begins parsing the document type declaration (<code class="docutils literal notranslate"><span class="pre"><!DOCTYPE</span> <span class="pre">...</span></code>). The <em>doctypeName</em> is provided exactly as presented. The <em>systemId</em> and <em>publicId</em> parameters give the system and public identifiers if specified, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if omitted. <em>has_internal_subset</em> will be true if the document contains and internal document declaration subset. This requires Expat version 1.2 or newer.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.EndDoctypeDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">EndDoctypeDeclHandler</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.EndDoctypeDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called when Expat is done parsing the document type declaration. This requires Expat version 1.2 or newer.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.ElementDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">ElementDeclHandler</code><span class="sig-paren">(</span><em>name</em>, <em>model</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.ElementDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called once for each element type declaration. <em>name</em> is the name of the element type, and <em>model</em> is a representation of the content model.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.AttlistDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">AttlistDeclHandler</code><span class="sig-paren">(</span><em>elname</em>, <em>attname</em>, <em>type</em>, <em>default</em>, <em>required</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.AttlistDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for each declared attribute for an element type. If an attribute list declaration declares three attributes, this handler is called three times, once for each attribute. <em>elname</em> is the name of the element to which the declaration applies and <em>attname</em> is the name of the attribute declared. The attribute type is a string passed as <em>type</em>; the possible values are <code class="docutils literal notranslate"><span class="pre">'CDATA'</span></code>, <code class="docutils literal notranslate"><span class="pre">'ID'</span></code>, <code class="docutils literal notranslate"><span class="pre">'IDREF'</span></code>, … <em>default</em> gives the default value for the attribute used when the attribute is not specified by the document instance, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there is no default value (<code class="docutils literal notranslate"><span class="pre">#IMPLIED</span></code> values). If the attribute is required to be given in the document instance, <em>required</em> will be true. This requires Expat version 1.95.0 or newer.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.StartElementHandler"> <code class="descclassname">xmlparser.</code><code class="descname">StartElementHandler</code><span class="sig-paren">(</span><em>name</em>, <em>attributes</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.StartElementHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for the start of every element. <em>name</em> is a string containing the element name, and <em>attributes</em> is a dictionary mapping attribute names to their values.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.EndElementHandler"> <code class="descclassname">xmlparser.</code><code class="descname">EndElementHandler</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.EndElementHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for the end of every element.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.ProcessingInstructionHandler"> <code class="descclassname">xmlparser.</code><code class="descname">ProcessingInstructionHandler</code><span class="sig-paren">(</span><em>target</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.ProcessingInstructionHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for every processing instruction.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.CharacterDataHandler"> <code class="descclassname">xmlparser.</code><code class="descname">CharacterDataHandler</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.CharacterDataHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for character data. This will be called for normal character data, CDATA marked content, and ignorable whitespace. Applications which must distinguish these cases can use the <a class="reference internal" href="#xml.parsers.expat.xmlparser.StartCdataSectionHandler" title="xml.parsers.expat.xmlparser.StartCdataSectionHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StartCdataSectionHandler</span></code></a>, <a class="reference internal" href="#xml.parsers.expat.xmlparser.EndCdataSectionHandler" title="xml.parsers.expat.xmlparser.EndCdataSectionHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EndCdataSectionHandler</span></code></a>, and <a class="reference internal" href="#xml.parsers.expat.xmlparser.ElementDeclHandler" title="xml.parsers.expat.xmlparser.ElementDeclHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ElementDeclHandler</span></code></a> callbacks to collect the required information.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.UnparsedEntityDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">UnparsedEntityDeclHandler</code><span class="sig-paren">(</span><em>entityName</em>, <em>base</em>, <em>systemId</em>, <em>publicId</em>, <em>notationName</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.UnparsedEntityDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for unparsed (NDATA) entity declarations. This is only present for version 1.2 of the Expat library; for more recent versions, use <a class="reference internal" href="#xml.parsers.expat.xmlparser.EntityDeclHandler" title="xml.parsers.expat.xmlparser.EntityDeclHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EntityDeclHandler</span></code></a> instead. (The underlying function in the Expat library has been declared obsolete.)</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.EntityDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">EntityDeclHandler</code><span class="sig-paren">(</span><em>entityName</em>, <em>is_parameter_entity</em>, <em>value</em>, <em>base</em>, <em>systemId</em>, <em>publicId</em>, <em>notationName</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.EntityDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for all entity declarations. For parameter and internal entities, <em>value</em> will be a string giving the declared contents of the entity; this will be <code class="docutils literal notranslate"><span class="pre">None</span></code> for external entities. The <em>notationName</em> parameter will be <code class="docutils literal notranslate"><span class="pre">None</span></code> for parsed entities, and the name of the notation for unparsed entities. <em>is_parameter_entity</em> will be true if the entity is a parameter entity or false for general entities (most applications only need to be concerned with general entities). This is only available starting with version 1.95.0 of the Expat library.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.NotationDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">NotationDeclHandler</code><span class="sig-paren">(</span><em>notationName</em>, <em>base</em>, <em>systemId</em>, <em>publicId</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.NotationDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for notation declarations. <em>notationName</em>, <em>base</em>, and <em>systemId</em>, and <em>publicId</em> are strings if given. If the public identifier is omitted, <em>publicId</em> will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.StartNamespaceDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">StartNamespaceDeclHandler</code><span class="sig-paren">(</span><em>prefix</em>, <em>uri</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.StartNamespaceDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called when an element contains a namespace declaration. Namespace declarations are processed before the <a class="reference internal" href="#xml.parsers.expat.xmlparser.StartElementHandler" title="xml.parsers.expat.xmlparser.StartElementHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StartElementHandler</span></code></a> is called for the element on which declarations are placed.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.EndNamespaceDeclHandler"> <code class="descclassname">xmlparser.</code><code class="descname">EndNamespaceDeclHandler</code><span class="sig-paren">(</span><em>prefix</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.EndNamespaceDeclHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called when the closing tag is reached for an element that contained a namespace declaration. This is called once for each namespace declaration on the element in the reverse of the order for which the <a class="reference internal" href="#xml.parsers.expat.xmlparser.StartNamespaceDeclHandler" title="xml.parsers.expat.xmlparser.StartNamespaceDeclHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StartNamespaceDeclHandler</span></code></a> was called to indicate the start of each namespace declaration’s scope. Calls to this handler are made after the corresponding <a class="reference internal" href="#xml.parsers.expat.xmlparser.EndElementHandler" title="xml.parsers.expat.xmlparser.EndElementHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EndElementHandler</span></code></a> for the end of the element.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.CommentHandler"> <code class="descclassname">xmlparser.</code><code class="descname">CommentHandler</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.CommentHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for comments. <em>data</em> is the text of the comment, excluding the leading <code class="docutils literal notranslate"><span class="pre">'<!-</span></code><code class="docutils literal notranslate"><span class="pre">-'</span></code> and trailing <code class="docutils literal notranslate"><span class="pre">'-</span></code><code class="docutils literal notranslate"><span class="pre">->'</span></code>.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.StartCdataSectionHandler"> <code class="descclassname">xmlparser.</code><code class="descname">StartCdataSectionHandler</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.StartCdataSectionHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called at the start of a CDATA section. This and <a class="reference internal" href="#xml.parsers.expat.xmlparser.EndCdataSectionHandler" title="xml.parsers.expat.xmlparser.EndCdataSectionHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EndCdataSectionHandler</span></code></a> are needed to be able to identify the syntactical start and end for CDATA sections.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.EndCdataSectionHandler"> <code class="descclassname">xmlparser.</code><code class="descname">EndCdataSectionHandler</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.EndCdataSectionHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called at the end of a CDATA section.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.DefaultHandler"> <code class="descclassname">xmlparser.</code><code class="descname">DefaultHandler</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.DefaultHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for any characters in the XML document for which no applicable handler has been specified. This means characters that are part of a construct which could be reported, but for which no handler has been supplied.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.DefaultHandlerExpand"> <code class="descclassname">xmlparser.</code><code class="descname">DefaultHandlerExpand</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.DefaultHandlerExpand" title="Permalink to this definition">¶</a></dt> <dd><p>This is the same as the <a class="reference internal" href="#xml.parsers.expat.xmlparser.DefaultHandler" title="xml.parsers.expat.xmlparser.DefaultHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">DefaultHandler()</span></code></a>, but doesn’t inhibit expansion of internal entities. The entity reference will not be passed to the default handler.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.NotStandaloneHandler"> <code class="descclassname">xmlparser.</code><code class="descname">NotStandaloneHandler</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.NotStandaloneHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called if the XML document hasn’t been declared as being a standalone document. This happens when there is an external subset or a reference to a parameter entity, but the XML declaration does not set standalone to <code class="docutils literal notranslate"><span class="pre">yes</span></code> in an XML declaration. If this handler returns <code class="docutils literal notranslate"><span class="pre">0</span></code>, then the parser will raise an <code class="xref py py-const docutils literal notranslate"><span class="pre">XML_ERROR_NOT_STANDALONE</span></code> error. If this handler is not set, no exception is raised by the parser for this condition.</p> </dd></dl> <dl class="method"> <dt id="xml.parsers.expat.xmlparser.ExternalEntityRefHandler"> <code class="descclassname">xmlparser.</code><code class="descname">ExternalEntityRefHandler</code><span class="sig-paren">(</span><em>context</em>, <em>base</em>, <em>systemId</em>, <em>publicId</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.parsers.expat.xmlparser.ExternalEntityRefHandler" title="Permalink to this definition">¶</a></dt> <dd><p>Called for references to external entities. <em>base</em> is the current base, as set by a previous call to <a class="reference internal" href="#xml.parsers.expat.xmlparser.SetBase" title="xml.parsers.expat.xmlparser.SetBase"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SetBase()</span></code></a>. The public and system identifiers, <em>systemId</em> and <em>publicId</em>, are strings if given; if the public identifier is not given, <em>publicId</em> will be <code class="docutils literal notranslate"><span class="pre">None</span></code>. The <em>context</em> value is opaque and should only be used as described below.</p> <p>For external entities to be parsed, this handler must be implemented. It is responsible for creating the sub-parser using <code class="docutils literal notranslate"><span class="pre">ExternalEntityParserCreate(context)</span></code>, initializing it with the appropriate callbacks, and parsing the entity. This handler should return an integer; if it returns <code class="docutils literal notranslate"><span class="pre">0</span></code>, the parser will raise an <code class="xref py py-const docutils literal notranslate"><span class="pre">XML_ERROR_EXTERNAL_ENTITY_HANDLING</span></code> error, otherwise parsing will continue.</p> <p>If this handler is not provided, external entities are reported by the <a class="reference internal" href="#xml.parsers.expat.xmlparser.DefaultHandler" title="xml.parsers.expat.xmlparser.DefaultHandler"><code class="xref py py-attr docutils literal notranslate"><span class="pre">DefaultHandler</span></code></a> callback, if provided.</p> </dd></dl> </div> <div class="section" id="expaterror-exceptions"> <span id="expaterror-objects"></span><h2>19.15.2. ExpatError Exceptions<a class="headerlink" href="#expaterror-exceptions" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExpatError</span></code></a> exceptions have a number of interesting attributes:</p> <dl class="attribute"> <dt id="xml.parsers.expat.ExpatError.code"> <code class="descclassname">ExpatError.</code><code class="descname">code</code><a class="headerlink" href="#xml.parsers.expat.ExpatError.code" title="Permalink to this definition">¶</a></dt> <dd><p>Expat’s internal error number for the specific error. This will match one of the constants defined in the <code class="docutils literal notranslate"><span class="pre">errors</span></code> object from this module.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.ExpatError.lineno"> <code class="descclassname">ExpatError.</code><code class="descname">lineno</code><a class="headerlink" href="#xml.parsers.expat.ExpatError.lineno" title="Permalink to this definition">¶</a></dt> <dd><p>Line number on which the error was detected. The first line is numbered <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="xml.parsers.expat.ExpatError.offset"> <code class="descclassname">ExpatError.</code><code class="descname">offset</code><a class="headerlink" href="#xml.parsers.expat.ExpatError.offset" title="Permalink to this definition">¶</a></dt> <dd><p>Character offset into the line where the error occurred. The first column is numbered <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> </div> <div class="section" id="example"> <span id="expat-example"></span><h2>19.15.3. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2> <p>The following program defines three handlers that just print out their arguments.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xml.parsers.expat</span> <span class="c1"># 3 handler functions</span> <span class="k">def</span> <span class="nf">start_element</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">'Start element:'</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attrs</span> <span class="k">def</span> <span class="nf">end_element</span><span class="p">(</span><span class="n">name</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">'End element:'</span><span class="p">,</span> <span class="n">name</span> <span class="k">def</span> <span class="nf">char_data</span><span class="p">(</span><span class="n">data</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">'Character data:'</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">p</span> <span class="o">=</span> <span class="n">xml</span><span class="o">.</span><span class="n">parsers</span><span class="o">.</span><span class="n">expat</span><span class="o">.</span><span class="n">ParserCreate</span><span class="p">()</span> <span class="n">p</span><span class="o">.</span><span class="n">StartElementHandler</span> <span class="o">=</span> <span class="n">start_element</span> <span class="n">p</span><span class="o">.</span><span class="n">EndElementHandler</span> <span class="o">=</span> <span class="n">end_element</span> <span class="n">p</span><span class="o">.</span><span class="n">CharacterDataHandler</span> <span class="o">=</span> <span class="n">char_data</span> <span class="n">p</span><span class="o">.</span><span class="n">Parse</span><span class="p">(</span><span class="s2">"""<?xml version="1.0"?></span> <span class="s2"><parent id="top"><child1 name="paul">Text goes here</child1></span> <span class="s2"><child2 name="fred">More text</child2></span> <span class="s2"></parent>"""</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> </pre></div> </div> <p>The output from this program is:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Start</span> <span class="n">element</span><span class="p">:</span> <span class="n">parent</span> <span class="p">{</span><span class="s1">'id'</span><span class="p">:</span> <span class="s1">'top'</span><span class="p">}</span> <span class="n">Start</span> <span class="n">element</span><span class="p">:</span> <span class="n">child1</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'paul'</span><span class="p">}</span> <span class="n">Character</span> <span class="n">data</span><span class="p">:</span> <span class="s1">'Text goes here'</span> <span class="n">End</span> <span class="n">element</span><span class="p">:</span> <span class="n">child1</span> <span class="n">Character</span> <span class="n">data</span><span class="p">:</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span> <span class="n">Start</span> <span class="n">element</span><span class="p">:</span> <span class="n">child2</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'fred'</span><span class="p">}</span> <span class="n">Character</span> <span class="n">data</span><span class="p">:</span> <span class="s1">'More text'</span> <span class="n">End</span> <span class="n">element</span><span class="p">:</span> <span class="n">child2</span> <span class="n">Character</span> <span class="n">data</span><span class="p">:</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span> <span class="n">End</span> <span class="n">element</span><span class="p">:</span> <span class="n">parent</span> </pre></div> </div> </div> <div class="section" id="content-model-descriptions"> <span id="expat-content-models"></span><h2>19.15.4. Content Model Descriptions<a class="headerlink" href="#content-model-descriptions" title="Permalink to this headline">¶</a></h2> <p>Content models are described using nested tuples. Each tuple contains four values: the type, the quantifier, the name, and a tuple of children. Children are simply additional content model descriptions.</p> <p>The values of the first two fields are constants defined in the <code class="docutils literal notranslate"><span class="pre">model</span></code> object of the <a class="reference internal" href="#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> module. These constants can be collected in two groups: the model type group and the quantifier group.</p> <p>The constants in the model type group are:</p> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CTYPE_ANY</code></dt> <dd><p>The element named by the model name was declared to have a content model of <code class="docutils literal notranslate"><span class="pre">ANY</span></code>.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CTYPE_CHOICE</code></dt> <dd><p>The named element allows a choice from a number of options; this is used for content models such as <code class="docutils literal notranslate"><span class="pre">(A</span> <span class="pre">|</span> <span class="pre">B</span> <span class="pre">|</span> <span class="pre">C)</span></code>.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CTYPE_EMPTY</code></dt> <dd><p>Elements which are declared to be <code class="docutils literal notranslate"><span class="pre">EMPTY</span></code> have this model type.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CTYPE_MIXED</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CTYPE_NAME</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CTYPE_SEQ</code></dt> <dd><p>Models which represent a series of models which follow one after the other are indicated with this model type. This is used for models such as <code class="docutils literal notranslate"><span class="pre">(A,</span> <span class="pre">B,</span> <span class="pre">C)</span></code>.</p> </dd></dl> <p>The constants in the quantifier group are:</p> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CQUANT_NONE</code></dt> <dd><p>No modifier is given, so it can appear exactly once, as for <code class="docutils literal notranslate"><span class="pre">A</span></code>.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CQUANT_OPT</code></dt> <dd><p>The model is optional: it can appear once or not at all, as for <code class="docutils literal notranslate"><span class="pre">A?</span></code>.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CQUANT_PLUS</code></dt> <dd><p>The model must occur one or more times (like <code class="docutils literal notranslate"><span class="pre">A+</span></code>).</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_CQUANT_REP</code></dt> <dd><p>The model must occur zero or more times, as for <code class="docutils literal notranslate"><span class="pre">A*</span></code>.</p> </dd></dl> </div> <div class="section" id="expat-error-constants"> <span id="expat-errors"></span><h2>19.15.5. Expat error constants<a class="headerlink" href="#expat-error-constants" title="Permalink to this headline">¶</a></h2> <p>The following constants are provided in the <code class="docutils literal notranslate"><span class="pre">errors</span></code> object of the <a class="reference internal" href="#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> module. These constants are useful in interpreting some of the attributes of the <a class="reference internal" href="#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExpatError</span></code></a> exception objects raised when an error has occurred.</p> <p>The <code class="docutils literal notranslate"><span class="pre">errors</span></code> object has the following attributes:</p> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_ASYNC_ENTITY</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF</code></dt> <dd><p>An entity reference in an attribute value referred to an external entity instead of an internal entity.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_BAD_CHAR_REF</code></dt> <dd><p>A character reference referred to a character which is illegal in XML (for example, character <code class="docutils literal notranslate"><span class="pre">0</span></code>, or ‘<code class="docutils literal notranslate"><span class="pre">&#0;</span></code>’).</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_BINARY_ENTITY_REF</code></dt> <dd><p>An entity reference referred to an entity which was declared with a notation, so cannot be parsed.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_DUPLICATE_ATTRIBUTE</code></dt> <dd><p>An attribute was used more than once in a start tag.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_INCORRECT_ENCODING</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_INVALID_TOKEN</code></dt> <dd><p>Raised when an input byte could not properly be assigned to a character; for example, a NUL byte (value <code class="docutils literal notranslate"><span class="pre">0</span></code>) in a UTF-8 input stream.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_JUNK_AFTER_DOC_ELEMENT</code></dt> <dd><p>Something other than whitespace occurred after the document element.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_MISPLACED_XML_PI</code></dt> <dd><p>An XML declaration was found somewhere other than the start of the input data.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_NO_ELEMENTS</code></dt> <dd><p>The document contains no elements (XML requires all documents to contain exactly one top-level element)..</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_NO_MEMORY</code></dt> <dd><p>Expat was not able to allocate memory internally.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_PARAM_ENTITY_REF</code></dt> <dd><p>A parameter entity reference was found where it was not allowed.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_PARTIAL_CHAR</code></dt> <dd><p>An incomplete character was found in the input.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_RECURSIVE_ENTITY_REF</code></dt> <dd><p>An entity reference contained another reference to the same entity; possibly via a different name, and possibly indirectly.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_SYNTAX</code></dt> <dd><p>Some unspecified syntax error was encountered.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_TAG_MISMATCH</code></dt> <dd><p>An end tag did not match the innermost open start tag.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNCLOSED_TOKEN</code></dt> <dd><p>Some token (such as a start tag) was not closed before the end of the stream or the next token was encountered.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNDEFINED_ENTITY</code></dt> <dd><p>A reference was made to an entity which was not defined.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNKNOWN_ENCODING</code></dt> <dd><p>The document encoding is not supported by Expat.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNCLOSED_CDATA_SECTION</code></dt> <dd><p>A CDATA marked section was not closed.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_EXTERNAL_ENTITY_HANDLING</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_NOT_STANDALONE</code></dt> <dd><p>The parser determined that the document was not “standalone” though it declared itself to be in the XML declaration, and the <code class="xref py py-attr docutils literal notranslate"><span class="pre">NotStandaloneHandler</span></code> was set and returned <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNEXPECTED_STATE</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_ENTITY_DECLARED_IN_PE</code></dt> <dd></dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_FEATURE_REQUIRES_XML_DTD</code></dt> <dd><p>An operation was requested that requires DTD support to be compiled in, but Expat was configured without DTD support. This should never be reported by a standard build of the <a class="reference internal" href="#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> module.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING</code></dt> <dd><p>A behavioral change was requested after parsing started that can only be changed before parsing has started. This is (currently) only raised by <code class="xref py py-meth docutils literal notranslate"><span class="pre">UseForeignDTD()</span></code>.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNBOUND_PREFIX</code></dt> <dd><p>An undeclared prefix was found when namespace processing was enabled.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_UNDECLARING_PREFIX</code></dt> <dd><p>The document attempted to remove the namespace declaration associated with a prefix.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_INCOMPLETE_PE</code></dt> <dd><p>A parameter entity contained incomplete markup.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_XML_DECL</code></dt> <dd><p>The document contained no document element at all.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_TEXT_DECL</code></dt> <dd><p>There was an error parsing a text declaration in an external entity.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_PUBLICID</code></dt> <dd><p>Characters were found in the public id that are not allowed.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_SUSPENDED</code></dt> <dd><p>The requested operation was made on a suspended parser, but isn’t allowed. This includes attempts to provide additional input or to stop the parser.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_NOT_SUSPENDED</code></dt> <dd><p>An attempt to resume the parser was made when the parser had not been suspended.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_ABORTED</code></dt> <dd><p>This should not be reported to Python applications.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_FINISHED</code></dt> <dd><p>The requested operation was made on a parser which was finished parsing input, but isn’t allowed. This includes attempts to provide additional input or to stop the parser.</p> </dd></dl> <dl class="data"> <dt> <code class="descclassname">xml.parsers.expat.</code><code class="descname">XML_ERROR_SUSPEND_PE</code></dt> <dd></dd></dl> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id3" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>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>.</td></tr> </tbody> </table> </div> </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.15. <code class="docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code> — Fast XML parsing using Expat</a><ul> <li><a class="reference internal" href="#xmlparser-objects">19.15.1. XMLParser Objects</a></li> <li><a class="reference internal" href="#expaterror-exceptions">19.15.2. ExpatError Exceptions</a></li> <li><a class="reference internal" href="#example">19.15.3. Example</a></li> <li><a class="reference internal" href="#content-model-descriptions">19.15.4. Content Model Descriptions</a></li> <li><a class="reference internal" href="#expat-error-constants">19.15.5. Expat error constants</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="xml.sax.reader.html" title="previous chapter">19.14. <code class="docutils literal notranslate"><span class="pre">xml.sax.xmlreader</span></code> — Interface for XML parsers</a></p> <h4>Next topic</h4> <p class="topless"><a href="internet.html" title="next chapter">20. Internet Protocols and Support</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/pyexpat.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#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="internet.html" title="20. Internet Protocols and Support" >next</a> |</li> <li class="right" > <a href="xml.sax.reader.html" title="19.14. xml.sax.xmlreader — Interface for XML parsers" >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.16 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" >19. Structured Markup Processing Tools</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, 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 Mar 27, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>
Save
cmd:
run