/Doc/library/pyexpat.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 895 lines · 534 code · 361 blank · 0 comment · 0 complexity · bd7cd540872d40aa041e1e4623c5c458 MD5 · raw file

  1. :mod:`xml.parsers.expat` --- Fast XML parsing using Expat
  2. =========================================================
  3. .. module:: xml.parsers.expat
  4. :synopsis: An interface to the Expat non-validating XML parser.
  5. .. moduleauthor:: Paul Prescod <paul@prescod.net>
  6. .. Markup notes:
  7. Many of the attributes of the XMLParser objects are callbacks. Since
  8. signature information must be presented, these are described using the method
  9. directive. Since they are attributes which are set by client code, in-text
  10. references to these attributes should be marked using the :member: role.
  11. .. versionadded:: 2.0
  12. .. index:: single: Expat
  13. The :mod:`xml.parsers.expat` module is a Python interface to the Expat
  14. non-validating XML parser. The module provides a single extension type,
  15. :class:`xmlparser`, that represents the current state of an XML parser. After
  16. an :class:`xmlparser` object has been created, various attributes of the object
  17. can be set to handler functions. When an XML document is then fed to the
  18. parser, the handler functions are called for the character data and markup in
  19. the XML document.
  20. .. index:: module: pyexpat
  21. This module uses the :mod:`pyexpat` module to provide access to the Expat
  22. parser. Direct use of the :mod:`pyexpat` module is deprecated.
  23. This module provides one exception and one type object:
  24. .. exception:: ExpatError
  25. The exception raised when Expat reports an error. See section
  26. :ref:`expaterror-objects` for more information on interpreting Expat errors.
  27. .. exception:: error
  28. Alias for :exc:`ExpatError`.
  29. .. data:: XMLParserType
  30. The type of the return values from the :func:`ParserCreate` function.
  31. The :mod:`xml.parsers.expat` module contains two functions:
  32. .. function:: ErrorString(errno)
  33. Returns an explanatory string for a given error number *errno*.
  34. .. function:: ParserCreate([encoding[, namespace_separator]])
  35. Creates and returns a new :class:`xmlparser` object. *encoding*, if specified,
  36. must be a string naming the encoding used by the XML data. Expat doesn't
  37. support as many encodings as Python does, and its repertoire of encodings can't
  38. be extended; it supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If
  39. *encoding* [1]_ is given it will override the implicit or explicit encoding of the
  40. document.
  41. Expat can optionally do XML namespace processing for you, enabled by providing a
  42. value for *namespace_separator*. The value must be a one-character string; a
  43. :exc:`ValueError` will be raised if the string has an illegal length (``None``
  44. is considered the same as omission). When namespace processing is enabled,
  45. element type names and attribute names that belong to a namespace will be
  46. expanded. The element name passed to the element handlers
  47. :attr:`StartElementHandler` and :attr:`EndElementHandler` will be the
  48. concatenation of the namespace URI, the namespace separator character, and the
  49. local part of the name. If the namespace separator is a zero byte (``chr(0)``)
  50. then the namespace URI and the local part will be concatenated without any
  51. separator.
  52. For example, if *namespace_separator* is set to a space character (``' '``) and
  53. the following document is parsed::
  54. <?xml version="1.0"?>
  55. <root xmlns = "http://default-namespace.org/"
  56. xmlns:py = "http://www.python.org/ns/">
  57. <py:elem1 />
  58. <elem2 xmlns="" />
  59. </root>
  60. :attr:`StartElementHandler` will receive the following strings for each
  61. element::
  62. http://default-namespace.org/ root
  63. http://www.python.org/ns/ elem1
  64. elem2
  65. .. seealso::
  66. `The Expat XML Parser <http://www.libexpat.org/>`_
  67. Home page of the Expat project.
  68. .. _xmlparser-objects:
  69. XMLParser Objects
  70. -----------------
  71. :class:`xmlparser` objects have the following methods:
  72. .. method:: xmlparser.Parse(data[, isfinal])
  73. Parses the contents of the string *data*, calling the appropriate handler
  74. functions to process the parsed data. *isfinal* must be true on the final call
  75. to this method. *data* can be the empty string at any time.
  76. .. method:: xmlparser.ParseFile(file)
  77. Parse XML data reading from the object *file*. *file* only needs to provide
  78. the ``read(nbytes)`` method, returning the empty string when there's no more
  79. data.
  80. .. method:: xmlparser.SetBase(base)
  81. Sets the base to be used for resolving relative URIs in system identifiers in
  82. declarations. Resolving relative identifiers is left to the application: this
  83. value will be passed through as the *base* argument to the
  84. :func:`ExternalEntityRefHandler`, :func:`NotationDeclHandler`, and
  85. :func:`UnparsedEntityDeclHandler` functions.
  86. .. method:: xmlparser.GetBase()
  87. Returns a string containing the base set by a previous call to :meth:`SetBase`,
  88. or ``None`` if :meth:`SetBase` hasn't been called.
  89. .. method:: xmlparser.GetInputContext()
  90. Returns the input data that generated the current event as a string. The data is
  91. in the encoding of the entity which contains the text. When called while an
  92. event handler is not active, the return value is ``None``.
  93. .. versionadded:: 2.1
  94. .. method:: xmlparser.ExternalEntityParserCreate(context[, encoding])
  95. Create a "child" parser which can be used to parse an external parsed entity
  96. referred to by content parsed by the parent parser. The *context* parameter
  97. should be the string passed to the :meth:`ExternalEntityRefHandler` handler
  98. function, described below. The child parser is created with the
  99. :attr:`ordered_attributes`, :attr:`returns_unicode` and
  100. :attr:`specified_attributes` set to the values of this parser.
  101. .. method:: xmlparser.UseForeignDTD([flag])
  102. Calling this with a true value for *flag* (the default) will cause Expat to call
  103. the :attr:`ExternalEntityRefHandler` with :const:`None` for all arguments to
  104. allow an alternate DTD to be loaded. If the document does not contain a
  105. document type declaration, the :attr:`ExternalEntityRefHandler` will still be
  106. called, but the :attr:`StartDoctypeDeclHandler` and
  107. :attr:`EndDoctypeDeclHandler` will not be called.
  108. Passing a false value for *flag* will cancel a previous call that passed a true
  109. value, but otherwise has no effect.
  110. This method can only be called before the :meth:`Parse` or :meth:`ParseFile`
  111. methods are called; calling it after either of those have been called causes
  112. :exc:`ExpatError` to be raised with the :attr:`code` attribute set to
  113. :const:`errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING`.
  114. .. versionadded:: 2.3
  115. :class:`xmlparser` objects have the following attributes:
  116. .. attribute:: xmlparser.buffer_size
  117. The size of the buffer used when :attr:`buffer_text` is true.
  118. A new buffer size can be set by assigning a new integer value
  119. to this attribute.
  120. When the size is changed, the buffer will be flushed.
  121. .. versionadded:: 2.3
  122. .. versionchanged:: 2.6
  123. The buffer size can now be changed.
  124. .. attribute:: xmlparser.buffer_text
  125. Setting this to true causes the :class:`xmlparser` object to buffer textual
  126. content returned by Expat to avoid multiple calls to the
  127. :meth:`CharacterDataHandler` callback whenever possible. This can improve
  128. performance substantially since Expat normally breaks character data into chunks
  129. at every line ending. This attribute is false by default, and may be changed at
  130. any time.
  131. .. versionadded:: 2.3
  132. .. attribute:: xmlparser.buffer_used
  133. If :attr:`buffer_text` is enabled, the number of bytes stored in the buffer.
  134. These bytes represent UTF-8 encoded text. This attribute has no meaningful
  135. interpretation when :attr:`buffer_text` is false.
  136. .. versionadded:: 2.3
  137. .. attribute:: xmlparser.ordered_attributes
  138. Setting this attribute to a non-zero integer causes the attributes to be
  139. reported as a list rather than a dictionary. The attributes are presented in
  140. the order found in the document text. For each attribute, two list entries are
  141. presented: the attribute name and the attribute value. (Older versions of this
  142. module also used this format.) By default, this attribute is false; it may be
  143. changed at any time.
  144. .. versionadded:: 2.1
  145. .. attribute:: xmlparser.returns_unicode
  146. If this attribute is set to a non-zero integer, the handler functions will be
  147. passed Unicode strings. If :attr:`returns_unicode` is :const:`False`, 8-bit
  148. strings containing UTF-8 encoded data will be passed to the handlers. This is
  149. :const:`True` by default when Python is built with Unicode support.
  150. .. versionchanged:: 1.6
  151. Can be changed at any time to affect the result type.
  152. .. attribute:: xmlparser.specified_attributes
  153. If set to a non-zero integer, the parser will report only those attributes which
  154. were specified in the document instance and not those which were derived from
  155. attribute declarations. Applications which set this need to be especially
  156. careful to use what additional information is available from the declarations as
  157. needed to comply with the standards for the behavior of XML processors. By
  158. default, this attribute is false; it may be changed at any time.
  159. .. versionadded:: 2.1
  160. The following attributes contain values relating to the most recent error
  161. encountered by an :class:`xmlparser` object, and will only have correct values
  162. once a call to :meth:`Parse` or :meth:`ParseFile` has raised a
  163. :exc:`xml.parsers.expat.ExpatError` exception.
  164. .. attribute:: xmlparser.ErrorByteIndex
  165. Byte index at which an error occurred.
  166. .. attribute:: xmlparser.ErrorCode
  167. Numeric code specifying the problem. This value can be passed to the
  168. :func:`ErrorString` function, or compared to one of the constants defined in the
  169. ``errors`` object.
  170. .. attribute:: xmlparser.ErrorColumnNumber
  171. Column number at which an error occurred.
  172. .. attribute:: xmlparser.ErrorLineNumber
  173. Line number at which an error occurred.
  174. The following attributes contain values relating to the current parse location
  175. in an :class:`xmlparser` object. During a callback reporting a parse event they
  176. indicate the location of the first of the sequence of characters that generated
  177. the event. When called outside of a callback, the position indicated will be
  178. just past the last parse event (regardless of whether there was an associated
  179. callback).
  180. .. versionadded:: 2.4
  181. .. attribute:: xmlparser.CurrentByteIndex
  182. Current byte index in the parser input.
  183. .. attribute:: xmlparser.CurrentColumnNumber
  184. Current column number in the parser input.
  185. .. attribute:: xmlparser.CurrentLineNumber
  186. Current line number in the parser input.
  187. Here is the list of handlers that can be set. To set a handler on an
  188. :class:`xmlparser` object *o*, use ``o.handlername = func``. *handlername* must
  189. be taken from the following list, and *func* must be a callable object accepting
  190. the correct number of arguments. The arguments are all strings, unless
  191. otherwise stated.
  192. .. method:: xmlparser.XmlDeclHandler(version, encoding, standalone)
  193. Called when the XML declaration is parsed. The XML declaration is the
  194. (optional) declaration of the applicable version of the XML recommendation, the
  195. encoding of the document text, and an optional "standalone" declaration.
  196. *version* and *encoding* will be strings of the type dictated by the
  197. :attr:`returns_unicode` attribute, and *standalone* will be ``1`` if the
  198. document is declared standalone, ``0`` if it is declared not to be standalone,
  199. or ``-1`` if the standalone clause was omitted. This is only available with
  200. Expat version 1.95.0 or newer.
  201. .. versionadded:: 2.1
  202. .. method:: xmlparser.StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)
  203. Called when Expat begins parsing the document type declaration (``<!DOCTYPE
  204. ...``). The *doctypeName* is provided exactly as presented. The *systemId* and
  205. *publicId* parameters give the system and public identifiers if specified, or
  206. ``None`` if omitted. *has_internal_subset* will be true if the document
  207. contains and internal document declaration subset. This requires Expat version
  208. 1.2 or newer.
  209. .. method:: xmlparser.EndDoctypeDeclHandler()
  210. Called when Expat is done parsing the document type declaration. This requires
  211. Expat version 1.2 or newer.
  212. .. method:: xmlparser.ElementDeclHandler(name, model)
  213. Called once for each element type declaration. *name* is the name of the
  214. element type, and *model* is a representation of the content model.
  215. .. method:: xmlparser.AttlistDeclHandler(elname, attname, type, default, required)
  216. Called for each declared attribute for an element type. If an attribute list
  217. declaration declares three attributes, this handler is called three times, once
  218. for each attribute. *elname* is the name of the element to which the
  219. declaration applies and *attname* is the name of the attribute declared. The
  220. attribute type is a string passed as *type*; the possible values are
  221. ``'CDATA'``, ``'ID'``, ``'IDREF'``, ... *default* gives the default value for
  222. the attribute used when the attribute is not specified by the document instance,
  223. or ``None`` if there is no default value (``#IMPLIED`` values). If the
  224. attribute is required to be given in the document instance, *required* will be
  225. true. This requires Expat version 1.95.0 or newer.
  226. .. method:: xmlparser.StartElementHandler(name, attributes)
  227. Called for the start of every element. *name* is a string containing the
  228. element name, and *attributes* is a dictionary mapping attribute names to their
  229. values.
  230. .. method:: xmlparser.EndElementHandler(name)
  231. Called for the end of every element.
  232. .. method:: xmlparser.ProcessingInstructionHandler(target, data)
  233. Called for every processing instruction.
  234. .. method:: xmlparser.CharacterDataHandler(data)
  235. Called for character data. This will be called for normal character data, CDATA
  236. marked content, and ignorable whitespace. Applications which must distinguish
  237. these cases can use the :attr:`StartCdataSectionHandler`,
  238. :attr:`EndCdataSectionHandler`, and :attr:`ElementDeclHandler` callbacks to
  239. collect the required information.
  240. .. method:: xmlparser.UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)
  241. Called for unparsed (NDATA) entity declarations. This is only present for
  242. version 1.2 of the Expat library; for more recent versions, use
  243. :attr:`EntityDeclHandler` instead. (The underlying function in the Expat
  244. library has been declared obsolete.)
  245. .. method:: xmlparser.EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)
  246. Called for all entity declarations. For parameter and internal entities,
  247. *value* will be a string giving the declared contents of the entity; this will
  248. be ``None`` for external entities. The *notationName* parameter will be
  249. ``None`` for parsed entities, and the name of the notation for unparsed
  250. entities. *is_parameter_entity* will be true if the entity is a parameter entity
  251. or false for general entities (most applications only need to be concerned with
  252. general entities). This is only available starting with version 1.95.0 of the
  253. Expat library.
  254. .. versionadded:: 2.1
  255. .. method:: xmlparser.NotationDeclHandler(notationName, base, systemId, publicId)
  256. Called for notation declarations. *notationName*, *base*, and *systemId*, and
  257. *publicId* are strings if given. If the public identifier is omitted,
  258. *publicId* will be ``None``.
  259. .. method:: xmlparser.StartNamespaceDeclHandler(prefix, uri)
  260. Called when an element contains a namespace declaration. Namespace declarations
  261. are processed before the :attr:`StartElementHandler` is called for the element
  262. on which declarations are placed.
  263. .. method:: xmlparser.EndNamespaceDeclHandler(prefix)
  264. Called when the closing tag is reached for an element that contained a
  265. namespace declaration. This is called once for each namespace declaration on
  266. the element in the reverse of the order for which the
  267. :attr:`StartNamespaceDeclHandler` was called to indicate the start of each
  268. namespace declaration's scope. Calls to this handler are made after the
  269. corresponding :attr:`EndElementHandler` for the end of the element.
  270. .. method:: xmlparser.CommentHandler(data)
  271. Called for comments. *data* is the text of the comment, excluding the leading
  272. '``<!-``\ ``-``' and trailing '``-``\ ``->``'.
  273. .. method:: xmlparser.StartCdataSectionHandler()
  274. Called at the start of a CDATA section. This and :attr:`EndCdataSectionHandler`
  275. are needed to be able to identify the syntactical start and end for CDATA
  276. sections.
  277. .. method:: xmlparser.EndCdataSectionHandler()
  278. Called at the end of a CDATA section.
  279. .. method:: xmlparser.DefaultHandler(data)
  280. Called for any characters in the XML document for which no applicable handler
  281. has been specified. This means characters that are part of a construct which
  282. could be reported, but for which no handler has been supplied.
  283. .. method:: xmlparser.DefaultHandlerExpand(data)
  284. This is the same as the :func:`DefaultHandler`, but doesn't inhibit expansion
  285. of internal entities. The entity reference will not be passed to the default
  286. handler.
  287. .. method:: xmlparser.NotStandaloneHandler()
  288. Called if the XML document hasn't been declared as being a standalone document.
  289. This happens when there is an external subset or a reference to a parameter
  290. entity, but the XML declaration does not set standalone to ``yes`` in an XML
  291. declaration. If this handler returns ``0``, then the parser will throw an
  292. :const:`XML_ERROR_NOT_STANDALONE` error. If this handler is not set, no
  293. exception is raised by the parser for this condition.
  294. .. method:: xmlparser.ExternalEntityRefHandler(context, base, systemId, publicId)
  295. Called for references to external entities. *base* is the current base, as set
  296. by a previous call to :meth:`SetBase`. The public and system identifiers,
  297. *systemId* and *publicId*, are strings if given; if the public identifier is not
  298. given, *publicId* will be ``None``. The *context* value is opaque and should
  299. only be used as described below.
  300. For external entities to be parsed, this handler must be implemented. It is
  301. responsible for creating the sub-parser using
  302. ``ExternalEntityParserCreate(context)``, initializing it with the appropriate
  303. callbacks, and parsing the entity. This handler should return an integer; if it
  304. returns ``0``, the parser will throw an
  305. :const:`XML_ERROR_EXTERNAL_ENTITY_HANDLING` error, otherwise parsing will
  306. continue.
  307. If this handler is not provided, external entities are reported by the
  308. :attr:`DefaultHandler` callback, if provided.
  309. .. _expaterror-objects:
  310. ExpatError Exceptions
  311. ---------------------
  312. .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
  313. :exc:`ExpatError` exceptions have a number of interesting attributes:
  314. .. attribute:: ExpatError.code
  315. Expat's internal error number for the specific error. This will match one of
  316. the constants defined in the ``errors`` object from this module.
  317. .. versionadded:: 2.1
  318. .. attribute:: ExpatError.lineno
  319. Line number on which the error was detected. The first line is numbered ``1``.
  320. .. versionadded:: 2.1
  321. .. attribute:: ExpatError.offset
  322. Character offset into the line where the error occurred. The first column is
  323. numbered ``0``.
  324. .. versionadded:: 2.1
  325. .. _expat-example:
  326. Example
  327. -------
  328. The following program defines three handlers that just print out their
  329. arguments. ::
  330. import xml.parsers.expat
  331. # 3 handler functions
  332. def start_element(name, attrs):
  333. print 'Start element:', name, attrs
  334. def end_element(name):
  335. print 'End element:', name
  336. def char_data(data):
  337. print 'Character data:', repr(data)
  338. p = xml.parsers.expat.ParserCreate()
  339. p.StartElementHandler = start_element
  340. p.EndElementHandler = end_element
  341. p.CharacterDataHandler = char_data
  342. p.Parse("""<?xml version="1.0"?>
  343. <parent id="top"><child1 name="paul">Text goes here</child1>
  344. <child2 name="fred">More text</child2>
  345. </parent>""", 1)
  346. The output from this program is::
  347. Start element: parent {'id': 'top'}
  348. Start element: child1 {'name': 'paul'}
  349. Character data: 'Text goes here'
  350. End element: child1
  351. Character data: '\n'
  352. Start element: child2 {'name': 'fred'}
  353. Character data: 'More text'
  354. End element: child2
  355. Character data: '\n'
  356. End element: parent
  357. .. _expat-content-models:
  358. Content Model Descriptions
  359. --------------------------
  360. .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
  361. Content modules are described using nested tuples. Each tuple contains four
  362. values: the type, the quantifier, the name, and a tuple of children. Children
  363. are simply additional content module descriptions.
  364. The values of the first two fields are constants defined in the ``model`` object
  365. of the :mod:`xml.parsers.expat` module. These constants can be collected in two
  366. groups: the model type group and the quantifier group.
  367. The constants in the model type group are:
  368. .. data:: XML_CTYPE_ANY
  369. :noindex:
  370. The element named by the model name was declared to have a content model of
  371. ``ANY``.
  372. .. data:: XML_CTYPE_CHOICE
  373. :noindex:
  374. The named element allows a choice from a number of options; this is used for
  375. content models such as ``(A | B | C)``.
  376. .. data:: XML_CTYPE_EMPTY
  377. :noindex:
  378. Elements which are declared to be ``EMPTY`` have this model type.
  379. .. data:: XML_CTYPE_MIXED
  380. :noindex:
  381. .. data:: XML_CTYPE_NAME
  382. :noindex:
  383. .. data:: XML_CTYPE_SEQ
  384. :noindex:
  385. Models which represent a series of models which follow one after the other are
  386. indicated with this model type. This is used for models such as ``(A, B, C)``.
  387. The constants in the quantifier group are:
  388. .. data:: XML_CQUANT_NONE
  389. :noindex:
  390. No modifier is given, so it can appear exactly once, as for ``A``.
  391. .. data:: XML_CQUANT_OPT
  392. :noindex:
  393. The model is optional: it can appear once or not at all, as for ``A?``.
  394. .. data:: XML_CQUANT_PLUS
  395. :noindex:
  396. The model must occur one or more times (like ``A+``).
  397. .. data:: XML_CQUANT_REP
  398. :noindex:
  399. The model must occur zero or more times, as for ``A*``.
  400. .. _expat-errors:
  401. Expat error constants
  402. ---------------------
  403. The following constants are provided in the ``errors`` object of the
  404. :mod:`xml.parsers.expat` module. These constants are useful in interpreting
  405. some of the attributes of the :exc:`ExpatError` exception objects raised when an
  406. error has occurred.
  407. The ``errors`` object has the following attributes:
  408. .. data:: XML_ERROR_ASYNC_ENTITY
  409. :noindex:
  410. .. data:: XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
  411. :noindex:
  412. An entity reference in an attribute value referred to an external entity instead
  413. of an internal entity.
  414. .. data:: XML_ERROR_BAD_CHAR_REF
  415. :noindex:
  416. A character reference referred to a character which is illegal in XML (for
  417. example, character ``0``, or '``&#0;``').
  418. .. data:: XML_ERROR_BINARY_ENTITY_REF
  419. :noindex:
  420. An entity reference referred to an entity which was declared with a notation, so
  421. cannot be parsed.
  422. .. data:: XML_ERROR_DUPLICATE_ATTRIBUTE
  423. :noindex:
  424. An attribute was used more than once in a start tag.
  425. .. data:: XML_ERROR_INCORRECT_ENCODING
  426. :noindex:
  427. .. data:: XML_ERROR_INVALID_TOKEN
  428. :noindex:
  429. Raised when an input byte could not properly be assigned to a character; for
  430. example, a NUL byte (value ``0``) in a UTF-8 input stream.
  431. .. data:: XML_ERROR_JUNK_AFTER_DOC_ELEMENT
  432. :noindex:
  433. Something other than whitespace occurred after the document element.
  434. .. data:: XML_ERROR_MISPLACED_XML_PI
  435. :noindex:
  436. An XML declaration was found somewhere other than the start of the input data.
  437. .. data:: XML_ERROR_NO_ELEMENTS
  438. :noindex:
  439. The document contains no elements (XML requires all documents to contain exactly
  440. one top-level element)..
  441. .. data:: XML_ERROR_NO_MEMORY
  442. :noindex:
  443. Expat was not able to allocate memory internally.
  444. .. data:: XML_ERROR_PARAM_ENTITY_REF
  445. :noindex:
  446. A parameter entity reference was found where it was not allowed.
  447. .. data:: XML_ERROR_PARTIAL_CHAR
  448. :noindex:
  449. An incomplete character was found in the input.
  450. .. data:: XML_ERROR_RECURSIVE_ENTITY_REF
  451. :noindex:
  452. An entity reference contained another reference to the same entity; possibly via
  453. a different name, and possibly indirectly.
  454. .. data:: XML_ERROR_SYNTAX
  455. :noindex:
  456. Some unspecified syntax error was encountered.
  457. .. data:: XML_ERROR_TAG_MISMATCH
  458. :noindex:
  459. An end tag did not match the innermost open start tag.
  460. .. data:: XML_ERROR_UNCLOSED_TOKEN
  461. :noindex:
  462. Some token (such as a start tag) was not closed before the end of the stream or
  463. the next token was encountered.
  464. .. data:: XML_ERROR_UNDEFINED_ENTITY
  465. :noindex:
  466. A reference was made to a entity which was not defined.
  467. .. data:: XML_ERROR_UNKNOWN_ENCODING
  468. :noindex:
  469. The document encoding is not supported by Expat.
  470. .. data:: XML_ERROR_UNCLOSED_CDATA_SECTION
  471. :noindex:
  472. A CDATA marked section was not closed.
  473. .. data:: XML_ERROR_EXTERNAL_ENTITY_HANDLING
  474. :noindex:
  475. .. data:: XML_ERROR_NOT_STANDALONE
  476. :noindex:
  477. The parser determined that the document was not "standalone" though it declared
  478. itself to be in the XML declaration, and the :attr:`NotStandaloneHandler` was
  479. set and returned ``0``.
  480. .. data:: XML_ERROR_UNEXPECTED_STATE
  481. :noindex:
  482. .. data:: XML_ERROR_ENTITY_DECLARED_IN_PE
  483. :noindex:
  484. .. data:: XML_ERROR_FEATURE_REQUIRES_XML_DTD
  485. :noindex:
  486. An operation was requested that requires DTD support to be compiled in, but
  487. Expat was configured without DTD support. This should never be reported by a
  488. standard build of the :mod:`xml.parsers.expat` module.
  489. .. data:: XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING
  490. :noindex:
  491. A behavioral change was requested after parsing started that can only be changed
  492. before parsing has started. This is (currently) only raised by
  493. :meth:`UseForeignDTD`.
  494. .. data:: XML_ERROR_UNBOUND_PREFIX
  495. :noindex:
  496. An undeclared prefix was found when namespace processing was enabled.
  497. .. data:: XML_ERROR_UNDECLARING_PREFIX
  498. :noindex:
  499. The document attempted to remove the namespace declaration associated with a
  500. prefix.
  501. .. data:: XML_ERROR_INCOMPLETE_PE
  502. :noindex:
  503. A parameter entity contained incomplete markup.
  504. .. data:: XML_ERROR_XML_DECL
  505. :noindex:
  506. The document contained no document element at all.
  507. .. data:: XML_ERROR_TEXT_DECL
  508. :noindex:
  509. There was an error parsing a text declaration in an external entity.
  510. .. data:: XML_ERROR_PUBLICID
  511. :noindex:
  512. Characters were found in the public id that are not allowed.
  513. .. data:: XML_ERROR_SUSPENDED
  514. :noindex:
  515. The requested operation was made on a suspended parser, but isn't allowed. This
  516. includes attempts to provide additional input or to stop the parser.
  517. .. data:: XML_ERROR_NOT_SUSPENDED
  518. :noindex:
  519. An attempt to resume the parser was made when the parser had not been suspended.
  520. .. data:: XML_ERROR_ABORTED
  521. :noindex:
  522. This should not be reported to Python applications.
  523. .. data:: XML_ERROR_FINISHED
  524. :noindex:
  525. The requested operation was made on a parser which was finished parsing input,
  526. but isn't allowed. This includes attempts to provide additional input or to
  527. stop the parser.
  528. .. data:: XML_ERROR_SUSPEND_PE
  529. :noindex:
  530. .. rubric:: Footnotes
  531. .. [#] The encoding string included in XML output should conform to the
  532. appropriate standards. For example, "UTF-8" is valid, but "UTF8" is
  533. not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl
  534. and http://www.iana.org/assignments/character-sets .