blob: 1eb9db112258cf2b6f0c14483470568481d2822c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`xml.parsers.expat` --- Fast XML parsing using Expat
3=========================================================
4
5.. module:: xml.parsers.expat
6 :synopsis: An interface to the Expat non-validating XML parser.
7.. moduleauthor:: Paul Prescod <paul@prescod.net>
8
9
Christian Heimes5b5e81c2007-12-31 16:14:33 +000010.. Markup notes:
11
12 Many of the attributes of the XMLParser objects are callbacks. Since
13 signature information must be presented, these are described using the method
14 directive. Since they are attributes which are set by client code, in-text
15 references to these attributes should be marked using the :member: role.
16
Georg Brandl116aa622007-08-15 14:28:22 +000017
Georg Brandl116aa622007-08-15 14:28:22 +000018.. index:: single: Expat
19
20The :mod:`xml.parsers.expat` module is a Python interface to the Expat
21non-validating XML parser. The module provides a single extension type,
22:class:`xmlparser`, that represents the current state of an XML parser. After
23an :class:`xmlparser` object has been created, various attributes of the object
24can be set to handler functions. When an XML document is then fed to the
25parser, the handler functions are called for the character data and markup in
26the XML document.
27
28.. index:: module: pyexpat
29
30This module uses the :mod:`pyexpat` module to provide access to the Expat
31parser. Direct use of the :mod:`pyexpat` module is deprecated.
32
33This module provides one exception and one type object:
34
35
36.. exception:: ExpatError
37
38 The exception raised when Expat reports an error. See section
39 :ref:`expaterror-objects` for more information on interpreting Expat errors.
40
41
42.. exception:: error
43
44 Alias for :exc:`ExpatError`.
45
46
47.. data:: XMLParserType
48
49 The type of the return values from the :func:`ParserCreate` function.
50
51The :mod:`xml.parsers.expat` module contains two functions:
52
53
54.. function:: ErrorString(errno)
55
56 Returns an explanatory string for a given error number *errno*.
57
58
59.. function:: ParserCreate([encoding[, namespace_separator]])
60
61 Creates and returns a new :class:`xmlparser` object. *encoding*, if specified,
62 must be a string naming the encoding used by the XML data. Expat doesn't
63 support as many encodings as Python does, and its repertoire of encodings can't
64 be extended; it supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If
Christian Heimesb186d002008-03-18 15:15:01 +000065 *encoding* [1]_ is given it will override the implicit or explicit encoding of the
Georg Brandl116aa622007-08-15 14:28:22 +000066 document.
67
68 Expat can optionally do XML namespace processing for you, enabled by providing a
69 value for *namespace_separator*. The value must be a one-character string; a
70 :exc:`ValueError` will be raised if the string has an illegal length (``None``
71 is considered the same as omission). When namespace processing is enabled,
72 element type names and attribute names that belong to a namespace will be
73 expanded. The element name passed to the element handlers
74 :attr:`StartElementHandler` and :attr:`EndElementHandler` will be the
75 concatenation of the namespace URI, the namespace separator character, and the
76 local part of the name. If the namespace separator is a zero byte (``chr(0)``)
77 then the namespace URI and the local part will be concatenated without any
78 separator.
79
80 For example, if *namespace_separator* is set to a space character (``' '``) and
81 the following document is parsed::
82
83 <?xml version="1.0"?>
84 <root xmlns = "http://default-namespace.org/"
85 xmlns:py = "http://www.python.org/ns/">
86 <py:elem1 />
87 <elem2 xmlns="" />
88 </root>
89
90 :attr:`StartElementHandler` will receive the following strings for each
91 element::
92
93 http://default-namespace.org/ root
94 http://www.python.org/ns/ elem1
95 elem2
96
97
98.. seealso::
99
100 `The Expat XML Parser <http://www.libexpat.org/>`_
101 Home page of the Expat project.
102
103
104.. _xmlparser-objects:
105
106XMLParser Objects
107-----------------
108
109:class:`xmlparser` objects have the following methods:
110
111
112.. method:: xmlparser.Parse(data[, isfinal])
113
114 Parses the contents of the string *data*, calling the appropriate handler
115 functions to process the parsed data. *isfinal* must be true on the final call
116 to this method. *data* can be the empty string at any time.
117
118
119.. method:: xmlparser.ParseFile(file)
120
121 Parse XML data reading from the object *file*. *file* only needs to provide
122 the ``read(nbytes)`` method, returning the empty string when there's no more
123 data.
124
125
126.. method:: xmlparser.SetBase(base)
127
128 Sets the base to be used for resolving relative URIs in system identifiers in
129 declarations. Resolving relative identifiers is left to the application: this
130 value will be passed through as the *base* argument to the
131 :func:`ExternalEntityRefHandler`, :func:`NotationDeclHandler`, and
132 :func:`UnparsedEntityDeclHandler` functions.
133
134
135.. method:: xmlparser.GetBase()
136
137 Returns a string containing the base set by a previous call to :meth:`SetBase`,
138 or ``None`` if :meth:`SetBase` hasn't been called.
139
140
141.. method:: xmlparser.GetInputContext()
142
143 Returns the input data that generated the current event as a string. The data is
144 in the encoding of the entity which contains the text. When called while an
145 event handler is not active, the return value is ``None``.
146
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148.. method:: xmlparser.ExternalEntityParserCreate(context[, encoding])
149
150 Create a "child" parser which can be used to parse an external parsed entity
151 referred to by content parsed by the parent parser. The *context* parameter
152 should be the string passed to the :meth:`ExternalEntityRefHandler` handler
153 function, described below. The child parser is created with the
154 :attr:`ordered_attributes` and :attr:`specified_attributes` set to the values of
155 this parser.
156
Antoine Pitrouc8123602011-01-05 18:41:10 +0000157.. method:: xmlparser.SetParamEntityParsing(flag)
158
159 Control parsing of parameter entities (including the external DTD subset).
160 Possible *flag* values are :const:`XML_PARAM_ENTITY_PARSING_NEVER`,
161 :const:`XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE` and
162 :const:`XML_PARAM_ENTITY_PARSING_ALWAYS`. Return true if setting the flag
163 was successful.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165.. method:: xmlparser.UseForeignDTD([flag])
166
167 Calling this with a true value for *flag* (the default) will cause Expat to call
168 the :attr:`ExternalEntityRefHandler` with :const:`None` for all arguments to
169 allow an alternate DTD to be loaded. If the document does not contain a
170 document type declaration, the :attr:`ExternalEntityRefHandler` will still be
171 called, but the :attr:`StartDoctypeDeclHandler` and
172 :attr:`EndDoctypeDeclHandler` will not be called.
173
174 Passing a false value for *flag* will cancel a previous call that passed a true
175 value, but otherwise has no effect.
176
177 This method can only be called before the :meth:`Parse` or :meth:`ParseFile`
178 methods are called; calling it after either of those have been called causes
179 :exc:`ExpatError` to be raised with the :attr:`code` attribute set to
180 :const:`errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING`.
181
Georg Brandl116aa622007-08-15 14:28:22 +0000182:class:`xmlparser` objects have the following attributes:
183
184
185.. attribute:: xmlparser.buffer_size
186
Georg Brandl48310cd2009-01-03 21:18:54 +0000187 The size of the buffer used when :attr:`buffer_text` is true.
188 A new buffer size can be set by assigning a new integer value
189 to this attribute.
Christian Heimes2380ac72008-01-09 00:17:24 +0000190 When the size is changed, the buffer will be flushed.
191
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193.. attribute:: xmlparser.buffer_text
194
195 Setting this to true causes the :class:`xmlparser` object to buffer textual
196 content returned by Expat to avoid multiple calls to the
197 :meth:`CharacterDataHandler` callback whenever possible. This can improve
198 performance substantially since Expat normally breaks character data into chunks
199 at every line ending. This attribute is false by default, and may be changed at
200 any time.
201
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203.. attribute:: xmlparser.buffer_used
204
205 If :attr:`buffer_text` is enabled, the number of bytes stored in the buffer.
206 These bytes represent UTF-8 encoded text. This attribute has no meaningful
207 interpretation when :attr:`buffer_text` is false.
208
Georg Brandl116aa622007-08-15 14:28:22 +0000209
210.. attribute:: xmlparser.ordered_attributes
211
212 Setting this attribute to a non-zero integer causes the attributes to be
213 reported as a list rather than a dictionary. The attributes are presented in
214 the order found in the document text. For each attribute, two list entries are
215 presented: the attribute name and the attribute value. (Older versions of this
216 module also used this format.) By default, this attribute is false; it may be
217 changed at any time.
218
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220.. attribute:: xmlparser.specified_attributes
221
222 If set to a non-zero integer, the parser will report only those attributes which
223 were specified in the document instance and not those which were derived from
224 attribute declarations. Applications which set this need to be especially
225 careful to use what additional information is available from the declarations as
226 needed to comply with the standards for the behavior of XML processors. By
227 default, this attribute is false; it may be changed at any time.
228
Georg Brandl116aa622007-08-15 14:28:22 +0000229
230The following attributes contain values relating to the most recent error
231encountered by an :class:`xmlparser` object, and will only have correct values
232once a call to :meth:`Parse` or :meth:`ParseFile` has raised a
233:exc:`xml.parsers.expat.ExpatError` exception.
234
235
236.. attribute:: xmlparser.ErrorByteIndex
237
238 Byte index at which an error occurred.
239
240
241.. attribute:: xmlparser.ErrorCode
242
243 Numeric code specifying the problem. This value can be passed to the
244 :func:`ErrorString` function, or compared to one of the constants defined in the
245 ``errors`` object.
246
247
248.. attribute:: xmlparser.ErrorColumnNumber
249
250 Column number at which an error occurred.
251
252
253.. attribute:: xmlparser.ErrorLineNumber
254
255 Line number at which an error occurred.
256
257The following attributes contain values relating to the current parse location
258in an :class:`xmlparser` object. During a callback reporting a parse event they
259indicate the location of the first of the sequence of characters that generated
260the event. When called outside of a callback, the position indicated will be
261just past the last parse event (regardless of whether there was an associated
262callback).
263
Georg Brandl116aa622007-08-15 14:28:22 +0000264
265.. attribute:: xmlparser.CurrentByteIndex
266
267 Current byte index in the parser input.
268
269
270.. attribute:: xmlparser.CurrentColumnNumber
271
272 Current column number in the parser input.
273
274
275.. attribute:: xmlparser.CurrentLineNumber
276
277 Current line number in the parser input.
278
279Here is the list of handlers that can be set. To set a handler on an
280:class:`xmlparser` object *o*, use ``o.handlername = func``. *handlername* must
281be taken from the following list, and *func* must be a callable object accepting
282the correct number of arguments. The arguments are all strings, unless
283otherwise stated.
284
285
286.. method:: xmlparser.XmlDeclHandler(version, encoding, standalone)
287
288 Called when the XML declaration is parsed. The XML declaration is the
289 (optional) declaration of the applicable version of the XML recommendation, the
290 encoding of the document text, and an optional "standalone" declaration.
291 *version* and *encoding* will be strings, and *standalone* will be ``1`` if the
292 document is declared standalone, ``0`` if it is declared not to be standalone,
293 or ``-1`` if the standalone clause was omitted. This is only available with
294 Expat version 1.95.0 or newer.
295
Georg Brandl116aa622007-08-15 14:28:22 +0000296
297.. method:: xmlparser.StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)
298
299 Called when Expat begins parsing the document type declaration (``<!DOCTYPE
300 ...``). The *doctypeName* is provided exactly as presented. The *systemId* and
301 *publicId* parameters give the system and public identifiers if specified, or
302 ``None`` if omitted. *has_internal_subset* will be true if the document
303 contains and internal document declaration subset. This requires Expat version
304 1.2 or newer.
305
306
307.. method:: xmlparser.EndDoctypeDeclHandler()
308
309 Called when Expat is done parsing the document type declaration. This requires
310 Expat version 1.2 or newer.
311
312
313.. method:: xmlparser.ElementDeclHandler(name, model)
314
315 Called once for each element type declaration. *name* is the name of the
316 element type, and *model* is a representation of the content model.
317
318
319.. method:: xmlparser.AttlistDeclHandler(elname, attname, type, default, required)
320
321 Called for each declared attribute for an element type. If an attribute list
322 declaration declares three attributes, this handler is called three times, once
323 for each attribute. *elname* is the name of the element to which the
324 declaration applies and *attname* is the name of the attribute declared. The
325 attribute type is a string passed as *type*; the possible values are
326 ``'CDATA'``, ``'ID'``, ``'IDREF'``, ... *default* gives the default value for
327 the attribute used when the attribute is not specified by the document instance,
328 or ``None`` if there is no default value (``#IMPLIED`` values). If the
329 attribute is required to be given in the document instance, *required* will be
330 true. This requires Expat version 1.95.0 or newer.
331
332
333.. method:: xmlparser.StartElementHandler(name, attributes)
334
335 Called for the start of every element. *name* is a string containing the
336 element name, and *attributes* is a dictionary mapping attribute names to their
337 values.
338
339
340.. method:: xmlparser.EndElementHandler(name)
341
342 Called for the end of every element.
343
344
345.. method:: xmlparser.ProcessingInstructionHandler(target, data)
346
347 Called for every processing instruction.
348
349
350.. method:: xmlparser.CharacterDataHandler(data)
351
352 Called for character data. This will be called for normal character data, CDATA
353 marked content, and ignorable whitespace. Applications which must distinguish
354 these cases can use the :attr:`StartCdataSectionHandler`,
355 :attr:`EndCdataSectionHandler`, and :attr:`ElementDeclHandler` callbacks to
356 collect the required information.
357
358
359.. method:: xmlparser.UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)
360
361 Called for unparsed (NDATA) entity declarations. This is only present for
362 version 1.2 of the Expat library; for more recent versions, use
363 :attr:`EntityDeclHandler` instead. (The underlying function in the Expat
364 library has been declared obsolete.)
365
366
367.. method:: xmlparser.EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)
368
369 Called for all entity declarations. For parameter and internal entities,
370 *value* will be a string giving the declared contents of the entity; this will
371 be ``None`` for external entities. The *notationName* parameter will be
372 ``None`` for parsed entities, and the name of the notation for unparsed
373 entities. *is_parameter_entity* will be true if the entity is a parameter entity
374 or false for general entities (most applications only need to be concerned with
375 general entities). This is only available starting with version 1.95.0 of the
376 Expat library.
377
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379.. method:: xmlparser.NotationDeclHandler(notationName, base, systemId, publicId)
380
381 Called for notation declarations. *notationName*, *base*, and *systemId*, and
382 *publicId* are strings if given. If the public identifier is omitted,
383 *publicId* will be ``None``.
384
385
386.. method:: xmlparser.StartNamespaceDeclHandler(prefix, uri)
387
388 Called when an element contains a namespace declaration. Namespace declarations
389 are processed before the :attr:`StartElementHandler` is called for the element
390 on which declarations are placed.
391
392
393.. method:: xmlparser.EndNamespaceDeclHandler(prefix)
394
395 Called when the closing tag is reached for an element that contained a
396 namespace declaration. This is called once for each namespace declaration on
397 the element in the reverse of the order for which the
398 :attr:`StartNamespaceDeclHandler` was called to indicate the start of each
399 namespace declaration's scope. Calls to this handler are made after the
400 corresponding :attr:`EndElementHandler` for the end of the element.
401
402
403.. method:: xmlparser.CommentHandler(data)
404
405 Called for comments. *data* is the text of the comment, excluding the leading
406 '``<!-``\ ``-``' and trailing '``-``\ ``->``'.
407
408
409.. method:: xmlparser.StartCdataSectionHandler()
410
411 Called at the start of a CDATA section. This and :attr:`EndCdataSectionHandler`
412 are needed to be able to identify the syntactical start and end for CDATA
413 sections.
414
415
416.. method:: xmlparser.EndCdataSectionHandler()
417
418 Called at the end of a CDATA section.
419
420
421.. method:: xmlparser.DefaultHandler(data)
422
423 Called for any characters in the XML document for which no applicable handler
424 has been specified. This means characters that are part of a construct which
425 could be reported, but for which no handler has been supplied.
426
427
428.. method:: xmlparser.DefaultHandlerExpand(data)
429
430 This is the same as the :func:`DefaultHandler`, but doesn't inhibit expansion
431 of internal entities. The entity reference will not be passed to the default
432 handler.
433
434
435.. method:: xmlparser.NotStandaloneHandler()
436
437 Called if the XML document hasn't been declared as being a standalone document.
438 This happens when there is an external subset or a reference to a parameter
439 entity, but the XML declaration does not set standalone to ``yes`` in an XML
Georg Brandl13f959b2010-10-06 08:35:38 +0000440 declaration. If this handler returns ``0``, then the parser will raise an
Georg Brandl116aa622007-08-15 14:28:22 +0000441 :const:`XML_ERROR_NOT_STANDALONE` error. If this handler is not set, no
442 exception is raised by the parser for this condition.
443
444
445.. method:: xmlparser.ExternalEntityRefHandler(context, base, systemId, publicId)
446
447 Called for references to external entities. *base* is the current base, as set
448 by a previous call to :meth:`SetBase`. The public and system identifiers,
449 *systemId* and *publicId*, are strings if given; if the public identifier is not
450 given, *publicId* will be ``None``. The *context* value is opaque and should
451 only be used as described below.
452
453 For external entities to be parsed, this handler must be implemented. It is
454 responsible for creating the sub-parser using
455 ``ExternalEntityParserCreate(context)``, initializing it with the appropriate
456 callbacks, and parsing the entity. This handler should return an integer; if it
Georg Brandl13f959b2010-10-06 08:35:38 +0000457 returns ``0``, the parser will raise an
Georg Brandl116aa622007-08-15 14:28:22 +0000458 :const:`XML_ERROR_EXTERNAL_ENTITY_HANDLING` error, otherwise parsing will
459 continue.
460
461 If this handler is not provided, external entities are reported by the
462 :attr:`DefaultHandler` callback, if provided.
463
464
465.. _expaterror-objects:
466
467ExpatError Exceptions
468---------------------
469
470.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
471
472
473:exc:`ExpatError` exceptions have a number of interesting attributes:
474
475
476.. attribute:: ExpatError.code
477
478 Expat's internal error number for the specific error. This will match one of
479 the constants defined in the ``errors`` object from this module.
480
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482.. attribute:: ExpatError.lineno
483
484 Line number on which the error was detected. The first line is numbered ``1``.
485
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487.. attribute:: ExpatError.offset
488
489 Character offset into the line where the error occurred. The first column is
490 numbered ``0``.
491
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493.. _expat-example:
494
495Example
496-------
497
498The following program defines three handlers that just print out their
499arguments. ::
500
501 import xml.parsers.expat
502
503 # 3 handler functions
504 def start_element(name, attrs):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000505 print('Start element:', name, attrs)
Georg Brandl116aa622007-08-15 14:28:22 +0000506 def end_element(name):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000507 print('End element:', name)
Georg Brandl116aa622007-08-15 14:28:22 +0000508 def char_data(data):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000509 print('Character data:', repr(data))
Georg Brandl116aa622007-08-15 14:28:22 +0000510
511 p = xml.parsers.expat.ParserCreate()
512
513 p.StartElementHandler = start_element
514 p.EndElementHandler = end_element
515 p.CharacterDataHandler = char_data
516
517 p.Parse("""<?xml version="1.0"?>
518 <parent id="top"><child1 name="paul">Text goes here</child1>
519 <child2 name="fred">More text</child2>
520 </parent>""", 1)
521
522The output from this program is::
523
524 Start element: parent {'id': 'top'}
525 Start element: child1 {'name': 'paul'}
526 Character data: 'Text goes here'
527 End element: child1
528 Character data: '\n'
529 Start element: child2 {'name': 'fred'}
530 Character data: 'More text'
531 End element: child2
532 Character data: '\n'
533 End element: parent
534
535
536.. _expat-content-models:
537
538Content Model Descriptions
539--------------------------
540
541.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
542
543
544Content modules are described using nested tuples. Each tuple contains four
545values: the type, the quantifier, the name, and a tuple of children. Children
546are simply additional content module descriptions.
547
548The values of the first two fields are constants defined in the ``model`` object
549of the :mod:`xml.parsers.expat` module. These constants can be collected in two
550groups: the model type group and the quantifier group.
551
552The constants in the model type group are:
553
554
555.. data:: XML_CTYPE_ANY
556 :noindex:
557
558 The element named by the model name was declared to have a content model of
559 ``ANY``.
560
561
562.. data:: XML_CTYPE_CHOICE
563 :noindex:
564
565 The named element allows a choice from a number of options; this is used for
566 content models such as ``(A | B | C)``.
567
568
569.. data:: XML_CTYPE_EMPTY
570 :noindex:
571
572 Elements which are declared to be ``EMPTY`` have this model type.
573
574
575.. data:: XML_CTYPE_MIXED
576 :noindex:
577
578
579.. data:: XML_CTYPE_NAME
580 :noindex:
581
582
583.. data:: XML_CTYPE_SEQ
584 :noindex:
585
586 Models which represent a series of models which follow one after the other are
587 indicated with this model type. This is used for models such as ``(A, B, C)``.
588
589The constants in the quantifier group are:
590
591
592.. data:: XML_CQUANT_NONE
593 :noindex:
594
595 No modifier is given, so it can appear exactly once, as for ``A``.
596
597
598.. data:: XML_CQUANT_OPT
599 :noindex:
600
601 The model is optional: it can appear once or not at all, as for ``A?``.
602
603
604.. data:: XML_CQUANT_PLUS
605 :noindex:
606
607 The model must occur one or more times (like ``A+``).
608
609
610.. data:: XML_CQUANT_REP
611 :noindex:
612
613 The model must occur zero or more times, as for ``A*``.
614
615
616.. _expat-errors:
617
618Expat error constants
619---------------------
620
621The following constants are provided in the ``errors`` object of the
622:mod:`xml.parsers.expat` module. These constants are useful in interpreting
623some of the attributes of the :exc:`ExpatError` exception objects raised when an
624error has occurred.
625
626The ``errors`` object has the following attributes:
627
628
629.. data:: XML_ERROR_ASYNC_ENTITY
630 :noindex:
631
632
633.. data:: XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
634 :noindex:
635
636 An entity reference in an attribute value referred to an external entity instead
637 of an internal entity.
638
639
640.. data:: XML_ERROR_BAD_CHAR_REF
641 :noindex:
642
643 A character reference referred to a character which is illegal in XML (for
644 example, character ``0``, or '``&#0;``').
645
646
647.. data:: XML_ERROR_BINARY_ENTITY_REF
648 :noindex:
649
650 An entity reference referred to an entity which was declared with a notation, so
651 cannot be parsed.
652
653
654.. data:: XML_ERROR_DUPLICATE_ATTRIBUTE
655 :noindex:
656
657 An attribute was used more than once in a start tag.
658
659
660.. data:: XML_ERROR_INCORRECT_ENCODING
661 :noindex:
662
663
664.. data:: XML_ERROR_INVALID_TOKEN
665 :noindex:
666
667 Raised when an input byte could not properly be assigned to a character; for
668 example, a NUL byte (value ``0``) in a UTF-8 input stream.
669
670
671.. data:: XML_ERROR_JUNK_AFTER_DOC_ELEMENT
672 :noindex:
673
674 Something other than whitespace occurred after the document element.
675
676
677.. data:: XML_ERROR_MISPLACED_XML_PI
678 :noindex:
679
680 An XML declaration was found somewhere other than the start of the input data.
681
682
683.. data:: XML_ERROR_NO_ELEMENTS
684 :noindex:
685
686 The document contains no elements (XML requires all documents to contain exactly
687 one top-level element)..
688
689
690.. data:: XML_ERROR_NO_MEMORY
691 :noindex:
692
693 Expat was not able to allocate memory internally.
694
695
696.. data:: XML_ERROR_PARAM_ENTITY_REF
697 :noindex:
698
699 A parameter entity reference was found where it was not allowed.
700
701
702.. data:: XML_ERROR_PARTIAL_CHAR
703 :noindex:
704
705 An incomplete character was found in the input.
706
707
708.. data:: XML_ERROR_RECURSIVE_ENTITY_REF
709 :noindex:
710
711 An entity reference contained another reference to the same entity; possibly via
712 a different name, and possibly indirectly.
713
714
715.. data:: XML_ERROR_SYNTAX
716 :noindex:
717
718 Some unspecified syntax error was encountered.
719
720
721.. data:: XML_ERROR_TAG_MISMATCH
722 :noindex:
723
724 An end tag did not match the innermost open start tag.
725
726
727.. data:: XML_ERROR_UNCLOSED_TOKEN
728 :noindex:
729
730 Some token (such as a start tag) was not closed before the end of the stream or
731 the next token was encountered.
732
733
734.. data:: XML_ERROR_UNDEFINED_ENTITY
735 :noindex:
736
737 A reference was made to a entity which was not defined.
738
739
740.. data:: XML_ERROR_UNKNOWN_ENCODING
741 :noindex:
742
743 The document encoding is not supported by Expat.
744
745
746.. data:: XML_ERROR_UNCLOSED_CDATA_SECTION
747 :noindex:
748
749 A CDATA marked section was not closed.
750
751
752.. data:: XML_ERROR_EXTERNAL_ENTITY_HANDLING
753 :noindex:
754
755
756.. data:: XML_ERROR_NOT_STANDALONE
757 :noindex:
758
759 The parser determined that the document was not "standalone" though it declared
760 itself to be in the XML declaration, and the :attr:`NotStandaloneHandler` was
761 set and returned ``0``.
762
763
764.. data:: XML_ERROR_UNEXPECTED_STATE
765 :noindex:
766
767
768.. data:: XML_ERROR_ENTITY_DECLARED_IN_PE
769 :noindex:
770
771
772.. data:: XML_ERROR_FEATURE_REQUIRES_XML_DTD
773 :noindex:
774
775 An operation was requested that requires DTD support to be compiled in, but
776 Expat was configured without DTD support. This should never be reported by a
777 standard build of the :mod:`xml.parsers.expat` module.
778
779
780.. data:: XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING
781 :noindex:
782
783 A behavioral change was requested after parsing started that can only be changed
784 before parsing has started. This is (currently) only raised by
785 :meth:`UseForeignDTD`.
786
787
788.. data:: XML_ERROR_UNBOUND_PREFIX
789 :noindex:
790
791 An undeclared prefix was found when namespace processing was enabled.
792
793
794.. data:: XML_ERROR_UNDECLARING_PREFIX
795 :noindex:
796
797 The document attempted to remove the namespace declaration associated with a
798 prefix.
799
800
801.. data:: XML_ERROR_INCOMPLETE_PE
802 :noindex:
803
804 A parameter entity contained incomplete markup.
805
806
807.. data:: XML_ERROR_XML_DECL
808 :noindex:
809
810 The document contained no document element at all.
811
812
813.. data:: XML_ERROR_TEXT_DECL
814 :noindex:
815
816 There was an error parsing a text declaration in an external entity.
817
818
819.. data:: XML_ERROR_PUBLICID
820 :noindex:
821
822 Characters were found in the public id that are not allowed.
823
824
825.. data:: XML_ERROR_SUSPENDED
826 :noindex:
827
828 The requested operation was made on a suspended parser, but isn't allowed. This
829 includes attempts to provide additional input or to stop the parser.
830
831
832.. data:: XML_ERROR_NOT_SUSPENDED
833 :noindex:
834
835 An attempt to resume the parser was made when the parser had not been suspended.
836
837
838.. data:: XML_ERROR_ABORTED
839 :noindex:
840
841 This should not be reported to Python applications.
842
843
844.. data:: XML_ERROR_FINISHED
845 :noindex:
846
847 The requested operation was made on a parser which was finished parsing input,
848 but isn't allowed. This includes attempts to provide additional input or to
849 stop the parser.
850
851
852.. data:: XML_ERROR_SUSPEND_PE
853 :noindex:
854
Christian Heimesb186d002008-03-18 15:15:01 +0000855
856.. rubric:: Footnotes
857
858.. [#] The encoding string included in XML output should conform to the
859 appropriate standards. For example, "UTF-8" is valid, but "UTF8" is
860 not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl
861 and http://www.iana.org/assignments/character-sets .
862