Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 1 | \section{\module{xml.parsers.expat} --- |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 2 | Fast XML parsing using Expat} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 3 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{xml.parsers.expat} |
| 5 | \modulesynopsis{An interface to the Expat non-validating XML parser.} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 6 | \moduleauthor{Paul Prescod}{paul@prescod.net} |
| 7 | \sectionauthor{A.M. Kuchling}{amk1@bigfoot.com} |
| 8 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 9 | \versionadded{2.0} |
| 10 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 11 | The \module{xml.parsers.expat} module is a Python interface to the |
| 12 | Expat\index{Expat} non-validating XML parser. |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 13 | The module provides a single extension type, \class{xmlparser}, that |
| 14 | represents the current state of an XML parser. After an |
| 15 | \class{xmlparser} object has been created, various attributes of the object |
| 16 | can be set to handler functions. When an XML document is then fed to |
| 17 | the parser, the handler functions are called for the character data |
| 18 | and markup in the XML document. |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 19 | |
| 20 | This module uses the \module{pyexpat}\refbimodindex{pyexpat} module to |
| 21 | provide access to the Expat parser. Direct use of the |
| 22 | \module{pyexpat} module is deprecated. |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 23 | |
| 24 | This module provides one exception and one type object: |
| 25 | |
| 26 | \begin{excdesc}{error} |
| 27 | The exception raised when Expat reports an error. |
| 28 | \end{excdesc} |
| 29 | |
| 30 | \begin{datadesc}{XMLParserType} |
| 31 | The type of the return values from the \function{ParserCreate()} |
| 32 | function. |
| 33 | \end{datadesc} |
| 34 | |
| 35 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 36 | The \module{xml.parsers.expat} module contains two functions: |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 37 | |
| 38 | \begin{funcdesc}{ErrorString}{errno} |
| 39 | Returns an explanatory string for a given error number \var{errno}. |
| 40 | \end{funcdesc} |
| 41 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 42 | \begin{funcdesc}{ParserCreate}{\optional{encoding\optional{, |
| 43 | namespace_separator}}} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 44 | Creates and returns a new \class{xmlparser} object. |
| 45 | \var{encoding}, if specified, must be a string naming the encoding |
| 46 | used by the XML data. Expat doesn't support as many encodings as |
| 47 | Python does, and its repertoire of encodings can't be extended; it |
| 48 | supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. |
| 49 | |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 50 | Expat can optionally do XML namespace processing for you, enabled by |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 51 | providing a value for \var{namespace_separator}. The value must be a |
| 52 | one-character string; a \exception{ValueError} will be raised if the |
| 53 | string has an illegal length (\code{None} is considered the same as |
| 54 | omission). When namespace processing is enabled, element type names |
| 55 | and attribute names that belong to a namespace will be expanded. The |
| 56 | element name passed to the element handlers |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 57 | \function{StartElementHandler()} and \function{EndElementHandler()} |
| 58 | will be the concatenation of the namespace URI, the namespace |
| 59 | separator character, and the local part of the name. If the namespace |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 60 | separator is a zero byte (\code{chr(0)}) then the namespace URI and |
| 61 | the local part will be concatenated without any separator. |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 62 | |
Fred Drake | 2fef3ab | 2000-11-28 06:38:22 +0000 | [diff] [blame] | 63 | For example, if \var{namespace_separator} is set to a space character |
| 64 | (\character{ }) and the following document is parsed: |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 65 | |
| 66 | \begin{verbatim} |
| 67 | <?xml version="1.0"?> |
| 68 | <root xmlns = "http://default-namespace.org/" |
| 69 | xmlns:py = "http://www.python.org/ns/"> |
| 70 | <py:elem1 /> |
| 71 | <elem2 xmlns="" /> |
| 72 | </root> |
| 73 | \end{verbatim} |
| 74 | |
Fred Drake | d79c33a | 2000-09-25 14:14:30 +0000 | [diff] [blame] | 75 | \function{StartElementHandler()} will receive the following strings |
| 76 | for each element: |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 77 | |
| 78 | \begin{verbatim} |
| 79 | http://default-namespace.org/ root |
| 80 | http://www.python.org/ns/ elem1 |
| 81 | elem2 |
| 82 | \end{verbatim} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 83 | \end{funcdesc} |
| 84 | |
Fred Drake | f08cbb1 | 2000-12-23 22:19:05 +0000 | [diff] [blame^] | 85 | |
| 86 | \subsection{XMLParser Objects \label{xmlparser-objects}} |
| 87 | |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 88 | \class{xmlparser} objects have the following methods: |
| 89 | |
Fred Drake | 2fef3ab | 2000-11-28 06:38:22 +0000 | [diff] [blame] | 90 | \begin{methoddesc}[xmlparser]{Parse}{data\optional{, isfinal}} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 91 | Parses the contents of the string \var{data}, calling the appropriate |
| 92 | handler functions to process the parsed data. \var{isfinal} must be |
Fred Drake | f08cbb1 | 2000-12-23 22:19:05 +0000 | [diff] [blame^] | 93 | true on the final call to this method. \var{data} can be the empty |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 94 | string at any time. |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 95 | \end{methoddesc} |
| 96 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 97 | \begin{methoddesc}[xmlparser]{ParseFile}{file} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 98 | Parse XML data reading from the object \var{file}. \var{file} only |
| 99 | needs to provide the \method{read(\var{nbytes})} method, returning the |
| 100 | empty string when there's no more data. |
| 101 | \end{methoddesc} |
| 102 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 103 | \begin{methoddesc}[xmlparser]{SetBase}{base} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 104 | Sets the base to be used for resolving relative URIs in system identifiers in |
| 105 | declarations. Resolving relative identifiers is left to the application: |
| 106 | this value will be passed through as the base argument to the |
| 107 | \function{ExternalEntityRefHandler}, \function{NotationDeclHandler}, |
| 108 | and \function{UnparsedEntityDeclHandler} functions. |
| 109 | \end{methoddesc} |
| 110 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 111 | \begin{methoddesc}[xmlparser]{GetBase}{} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 112 | Returns a string containing the base set by a previous call to |
| 113 | \method{SetBase()}, or \code{None} if |
| 114 | \method{SetBase()} hasn't been called. |
| 115 | \end{methoddesc} |
| 116 | |
Fred Drake | f08cbb1 | 2000-12-23 22:19:05 +0000 | [diff] [blame^] | 117 | \begin{methoddesc}[xmlparser]{ExternalEntityParserCreate}{context\optional{, |
| 118 | encoding}} |
| 119 | Create a ``child'' parser which can be used to parse an external |
| 120 | parsed entity referred to by content parsed by the parent parser. The |
| 121 | \var{content} parameter should be the string passed to the |
| 122 | \method{ExternalEntityRefHandler()} handler function, described below. |
| 123 | \end{methoddesc} |
| 124 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 125 | |
Fred Drake | d79c33a | 2000-09-25 14:14:30 +0000 | [diff] [blame] | 126 | \class{xmlparser} objects have the following attributes: |
Andrew M. Kuchling | 0690c86 | 2000-08-17 23:15:21 +0000 | [diff] [blame] | 127 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 128 | \begin{memberdesc}[xmlparser]{returns_unicode} |
Andrew M. Kuchling | 0690c86 | 2000-08-17 23:15:21 +0000 | [diff] [blame] | 129 | If this attribute is set to 1, the handler functions will be passed |
| 130 | Unicode strings. If \member{returns_unicode} is 0, 8-bit strings |
| 131 | containing UTF-8 encoded data will be passed to the handlers. |
Fred Drake | b62966c | 2000-12-07 00:00:21 +0000 | [diff] [blame] | 132 | \versionchanged[Can be changed at any time to affect the result |
| 133 | type.]{1.6} |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 134 | \end{memberdesc} |
Andrew M. Kuchling | 0690c86 | 2000-08-17 23:15:21 +0000 | [diff] [blame] | 135 | |
| 136 | The following attributes contain values relating to the most recent |
| 137 | error encountered by an \class{xmlparser} object, and will only have |
| 138 | correct values once a call to \method{Parse()} or \method{ParseFile()} |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 139 | has raised a \exception{xml.parsers.expat.error} exception. |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 140 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 141 | \begin{memberdesc}[xmlparser]{ErrorByteIndex} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 142 | Byte index at which an error occurred. |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 143 | \end{memberdesc} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 144 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 145 | \begin{memberdesc}[xmlparser]{ErrorCode} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 146 | Numeric code specifying the problem. This value can be passed to the |
| 147 | \function{ErrorString()} function, or compared to one of the constants |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 148 | defined in the \module{errors} object. |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 149 | \end{memberdesc} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 150 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 151 | \begin{memberdesc}[xmlparser]{ErrorColumnNumber} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 152 | Column number at which an error occurred. |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 153 | \end{memberdesc} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 154 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 155 | \begin{memberdesc}[xmlparser]{ErrorLineNumber} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 156 | Line number at which an error occurred. |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 157 | \end{memberdesc} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 158 | |
| 159 | Here is the list of handlers that can be set. To set a handler on an |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 160 | \class{xmlparser} object \var{o}, use |
| 161 | \code{\var{o}.\var{handlername} = \var{func}}. \var{handlername} must |
| 162 | be taken from the following list, and \var{func} must be a callable |
| 163 | object accepting the correct number of arguments. The arguments are |
| 164 | all strings, unless otherwise stated. |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 165 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 166 | \begin{methoddesc}[xmlparser]{StartElementHandler}{name, attributes} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 167 | Called for the start of every element. \var{name} is a string |
| 168 | containing the element name, and \var{attributes} is a dictionary |
| 169 | mapping attribute names to their values. |
| 170 | \end{methoddesc} |
| 171 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 172 | \begin{methoddesc}[xmlparser]{EndElementHandler}{name} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 173 | Called for the end of every element. |
| 174 | \end{methoddesc} |
| 175 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 176 | \begin{methoddesc}[xmlparser]{ProcessingInstructionHandler}{target, data} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 177 | Called for every processing instruction. |
| 178 | \end{methoddesc} |
| 179 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 180 | \begin{methoddesc}[xmlparser]{CharacterDataHandler}{data} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 181 | Called for character data. |
| 182 | \end{methoddesc} |
| 183 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 184 | \begin{methoddesc}[xmlparser]{UnparsedEntityDeclHandler}{entityName, base, |
| 185 | systemId, publicId, |
| 186 | notationName} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 187 | Called for unparsed (NDATA) entity declarations. |
| 188 | \end{methoddesc} |
| 189 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 190 | \begin{methoddesc}[xmlparser]{NotationDeclHandler}{notationName, base, |
| 191 | systemId, publicId} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 192 | Called for notation declarations. |
| 193 | \end{methoddesc} |
| 194 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 195 | \begin{methoddesc}[xmlparser]{StartNamespaceDeclHandler}{prefix, uri} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 196 | Called when an element contains a namespace declaration. |
| 197 | \end{methoddesc} |
| 198 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 199 | \begin{methoddesc}[xmlparser]{EndNamespaceDeclHandler}{prefix} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 200 | Called when the closing tag is reached for an element |
| 201 | that contained a namespace declaration. |
| 202 | \end{methoddesc} |
| 203 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 204 | \begin{methoddesc}[xmlparser]{CommentHandler}{data} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 205 | Called for comments. |
| 206 | \end{methoddesc} |
| 207 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 208 | \begin{methoddesc}[xmlparser]{StartCdataSectionHandler}{} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 209 | Called at the start of a CDATA section. |
| 210 | \end{methoddesc} |
| 211 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 212 | \begin{methoddesc}[xmlparser]{EndCdataSectionHandler}{} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 213 | Called at the end of a CDATA section. |
| 214 | \end{methoddesc} |
| 215 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 216 | \begin{methoddesc}[xmlparser]{DefaultHandler}{data} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 217 | Called for any characters in the XML document for |
| 218 | which no applicable handler has been specified. This means |
| 219 | characters that are part of a construct which could be reported, but |
| 220 | for which no handler has been supplied. |
| 221 | \end{methoddesc} |
| 222 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 223 | \begin{methoddesc}[xmlparser]{DefaultHandlerExpand}{data} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 224 | This is the same as the \function{DefaultHandler}, |
| 225 | but doesn't inhibit expansion of internal entities. |
| 226 | The entity reference will not be passed to the default handler. |
| 227 | \end{methoddesc} |
| 228 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 229 | \begin{methoddesc}[xmlparser]{NotStandaloneHandler}{} |
Fred Drake | d79c33a | 2000-09-25 14:14:30 +0000 | [diff] [blame] | 230 | Called if the XML document hasn't been declared as being a standalone |
| 231 | document. |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 232 | \end{methoddesc} |
| 233 | |
Fred Drake | efffe8e | 2000-10-29 05:10:30 +0000 | [diff] [blame] | 234 | \begin{methoddesc}[xmlparser]{ExternalEntityRefHandler}{context, base, |
| 235 | systemId, publicId} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 236 | Called for references to external entities. |
| 237 | \end{methoddesc} |
| 238 | |
| 239 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 240 | \subsection{Example \label{expat-example}} |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 241 | |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 242 | The following program defines three handlers that just print out their |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 243 | arguments. |
| 244 | |
| 245 | \begin{verbatim} |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 246 | import xml.parsers.expat |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 247 | |
| 248 | # 3 handler functions |
| 249 | def start_element(name, attrs): |
| 250 | print 'Start element:', name, attrs |
| 251 | def end_element(name): |
| 252 | print 'End element:', name |
| 253 | def char_data(data): |
| 254 | print 'Character data:', repr(data) |
| 255 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 256 | p = xml.parsers.expat.ParserCreate() |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 257 | |
| 258 | p.StartElementHandler = start_element |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 259 | p.EndElementHandler = end_element |
| 260 | p.CharacterDataHandler = char_data |
Andrew M. Kuchling | 6b14eeb | 2000-06-11 02:42:07 +0000 | [diff] [blame] | 261 | |
| 262 | p.Parse("""<?xml version="1.0"?> |
| 263 | <parent id="top"><child1 name="paul">Text goes here</child1> |
| 264 | <child2 name="fred">More text</child2> |
| 265 | </parent>""") |
| 266 | \end{verbatim} |
| 267 | |
| 268 | The output from this program is: |
| 269 | |
| 270 | \begin{verbatim} |
| 271 | Start element: parent {'id': 'top'} |
| 272 | Start element: child1 {'name': 'paul'} |
| 273 | Character data: 'Text goes here' |
| 274 | End element: child1 |
| 275 | Character data: '\012' |
| 276 | Start element: child2 {'name': 'fred'} |
| 277 | Character data: 'More text' |
| 278 | End element: child2 |
| 279 | Character data: '\012' |
| 280 | End element: parent |
| 281 | \end{verbatim} |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 282 | |
| 283 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 284 | \subsection{Expat error constants \label{expat-errors}} |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 285 | \sectionauthor{A.M. Kuchling}{amk1@bigfoot.com} |
| 286 | |
| 287 | The following table lists the error constants in the |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 288 | \code{errors} object of the \module{xml.parsers.expat} module. These |
| 289 | constants are useful in interpreting some of the attributes of the |
| 290 | parser object after an error has occurred. |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 291 | |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 292 | The \code{errors} object has the following attributes: |
Fred Drake | c05cbb0 | 2000-07-05 02:03:34 +0000 | [diff] [blame] | 293 | |
Fred Drake | acab3d6 | 2000-07-11 16:30:30 +0000 | [diff] [blame] | 294 | \begin{datadesc}{XML_ERROR_ASYNC_ENTITY} |
| 295 | \end{datadesc} |
| 296 | |
| 297 | \begin{datadesc}{XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF} |
| 298 | \end{datadesc} |
| 299 | |
| 300 | \begin{datadesc}{XML_ERROR_BAD_CHAR_REF} |
| 301 | \end{datadesc} |
| 302 | |
| 303 | \begin{datadesc}{XML_ERROR_BINARY_ENTITY_REF} |
| 304 | \end{datadesc} |
| 305 | |
| 306 | \begin{datadesc}{XML_ERROR_DUPLICATE_ATTRIBUTE} |
| 307 | An attribute was used more than once in a start tag. |
| 308 | \end{datadesc} |
| 309 | |
| 310 | \begin{datadesc}{XML_ERROR_INCORRECT_ENCODING} |
| 311 | \end{datadesc} |
| 312 | |
| 313 | \begin{datadesc}{XML_ERROR_INVALID_TOKEN} |
| 314 | \end{datadesc} |
| 315 | |
| 316 | \begin{datadesc}{XML_ERROR_JUNK_AFTER_DOC_ELEMENT} |
| 317 | Something other than whitespace occurred after the document element. |
| 318 | \end{datadesc} |
| 319 | |
| 320 | \begin{datadesc}{XML_ERROR_MISPLACED_XML_PI} |
| 321 | \end{datadesc} |
| 322 | |
| 323 | \begin{datadesc}{XML_ERROR_NO_ELEMENTS} |
| 324 | \end{datadesc} |
| 325 | |
| 326 | \begin{datadesc}{XML_ERROR_NO_MEMORY} |
| 327 | Expat was not able to allocate memory internally. |
| 328 | \end{datadesc} |
| 329 | |
| 330 | \begin{datadesc}{XML_ERROR_PARAM_ENTITY_REF} |
| 331 | \end{datadesc} |
| 332 | |
| 333 | \begin{datadesc}{XML_ERROR_PARTIAL_CHAR} |
| 334 | \end{datadesc} |
| 335 | |
| 336 | \begin{datadesc}{XML_ERROR_RECURSIVE_ENTITY_REF} |
| 337 | \end{datadesc} |
| 338 | |
| 339 | \begin{datadesc}{XML_ERROR_SYNTAX} |
| 340 | Some unspecified syntax error was encountered. |
| 341 | \end{datadesc} |
| 342 | |
| 343 | \begin{datadesc}{XML_ERROR_TAG_MISMATCH} |
| 344 | An end tag did not match the innermost open start tag. |
| 345 | \end{datadesc} |
| 346 | |
| 347 | \begin{datadesc}{XML_ERROR_UNCLOSED_TOKEN} |
| 348 | \end{datadesc} |
| 349 | |
| 350 | \begin{datadesc}{XML_ERROR_UNDEFINED_ENTITY} |
| 351 | A reference was made to a entity which was not defined. |
| 352 | \end{datadesc} |
| 353 | |
| 354 | \begin{datadesc}{XML_ERROR_UNKNOWN_ENCODING} |
| 355 | The document encoding is not supported by Expat. |
| 356 | \end{datadesc} |