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