1 \section{\module{xml.sax.xmlreader
} ---
2 Interface for XML parsers
}
4 \declaremodule{standard
}{xml.sax.xmlreader
}
5 \modulesynopsis{Interface which SAX-compliant XML parsers must implement.
}
6 \sectionauthor{Martin v. L\"owis
}{loewis@informatik.hu-berlin.de
}
7 \moduleauthor{Lars Marius Garshol
}{larsga@garshol.priv.no
}
12 SAX parsers implement the
\class{XMLReader
} interface. They are
13 implemented in a Python module, which must provide a function
14 \function{create_parser()
}. This function is invoked by
15 \function{xml.sax.make_parser()
} with no arguments to create a new
18 \begin{classdesc
}{XMLReader
}{}
19 Base class which can be inherited by SAX parsers.
22 \begin{classdesc
}{IncrementalParser
}{}
23 In some cases, it is desirable not to parse an input source at once,
24 but to feed chunks of the
document as they get available. Note that
25 the reader will normally not read the entire file, but read it in
26 chunks as well; still
\method{parse()
} won't return until the entire
27 document is processed. So these interfaces should be used if the
28 blocking behaviour of
\method{parse()
} is not desirable.
30 When the parser is instantiated it is ready to begin accepting data
31 from the feed method immediately. After parsing has been finished
32 with a call to close the reset method must be called to make the
33 parser ready to accept new data, either from feed or using the parse
36 Note that these methods must
\emph{not
} be called during parsing,
37 that is, after parse has been called and before it returns.
39 By default, the class also implements the parse method of the
40 XMLReader interface using the feed, close and reset methods of the
41 IncrementalParser interface as a convenience to SAX
2.0 driver
45 \begin{classdesc
}{Locator
}{}
46 Interface for associating a SAX event with a
document location. A
47 locator object will return valid results only during calls to
48 DocumentHandler methods; at any other time, the results are
49 unpredictable. If information is not available, methods may return
53 \begin{classdesc
}{InputSource
}{\optional{systemId
}}
54 Encapsulation of the information needed by the
\class{XMLReader
} to
57 This class may include information about the public identifier,
58 system identifier, byte stream (possibly with character encoding
59 information) and/or the character stream of an entity.
61 Applications will create objects of this class for use in the
62 \method{XMLReader.parse()
} method and for returning from
63 EntityResolver.resolveEntity.
65 An
\class{InputSource
} belongs to the application, the
66 \class{XMLReader
} is not allowed to modify
\class{InputSource
} objects
67 passed to it from the application, although it may make copies and
71 \begin{classdesc
}{AttributesImpl
}{attrs
}
72 This is a dictionary-like object which represents the element
73 attributes in a
\method{startElement()
} call. In addition to the
74 most useful dictionary operations, it supports a number of other
75 methods as described below. Objects of this class should be
76 instantiated by readers;
\var{attrs
} must be a dictionary-like
80 \begin{classdesc
}{AttributesNSImpl
}{attrs, qnames
}
81 Namespace-aware variant of attributes, which will be passed to
82 \method{startElementNS()
}. It is derived from
\class{AttributesImpl
},
83 but understands attribute names as two-tuples of
\var{namespaceURI
}
84 and
\var{localname
}. In addition, it provides a number of methods
85 expecting qualified names as they appear in the original
document.
89 \subsection{XMLReader Objects
\label{xmlreader-objects
}}
91 The
\class{XMLReader
} interface supports the following methods:
93 \begin{methoddesc
}[XMLReader
]{parse
}{source
}
94 Process an input source, producing SAX events. The
\var{source
}
95 object can be a system identifier (a string identifying the
96 input source -- typically a file name or an URL), a file-like
97 object, or an
\class{InputSource
} object. When
\method{parse()
}
98 returns, the input is completely processed, and the parser object
99 can be discarded or reset. As a limitation, the current implementation
100 only accepts byte streams; processing of character streams is for
104 \begin{methoddesc
}[XMLReader
]{getContentHandler
}{}
105 Return the current
\class{ContentHandler
}.
108 \begin{methoddesc
}[XMLReader
]{setContentHandler
}{handler
}
109 Set the current
\class{ContentHandler
}. If no
110 \class{ContentHandler
} is set, content events will be discarded.
113 \begin{methoddesc
}[XMLReader
]{getDTDHandler
}{}
114 Return the current
\class{DTDHandler
}.
117 \begin{methoddesc
}[XMLReader
]{setDTDHandler
}{handler
}
118 Set the current
\class{DTDHandler
}. If no
\class{DTDHandler
} is
119 set, DTD events will be discarded.
122 \begin{methoddesc
}[XMLReader
]{getEntityResolver
}{}
123 Return the current
\class{EntityResolver
}.
126 \begin{methoddesc
}[XMLReader
]{setEntityResolver
}{handler
}
127 Set the current
\class{EntityResolver
}. If no
128 \class{EntityResolver
} is set, attempts to resolve an external
129 entity will result in opening the system identifier for the entity,
130 and fail if it is not available.
133 \begin{methoddesc
}[XMLReader
]{getErrorHandler
}{}
134 Return the current
\class{ErrorHandler
}.
137 \begin{methoddesc
}[XMLReader
]{setErrorHandler
}{handler
}
138 Set the current error handler. If no
\class{ErrorHandler
} is set,
139 errors will be raised as exceptions, and warnings will be printed.
142 \begin{methoddesc
}[XMLReader
]{setLocale
}{locale
}
143 Allow an application to set the locale for errors and warnings.
145 SAX parsers are not required to provide localization for errors and
146 warnings; if they cannot support the requested locale, however, they
147 must throw a SAX exception. Applications may request a locale change
148 in the middle of a parse.
151 \begin{methoddesc
}[XMLReader
]{getFeature
}{featurename
}
152 Return the current setting for feature
\var{featurename
}. If the
153 feature is not recognized,
\exception{SAXNotRecognizedException
} is
154 raised. The well-known featurenames are listed in the module
155 \module{xml.sax.handler
}.
158 \begin{methoddesc
}[XMLReader
]{setFeature
}{featurename, value
}
159 Set the
\var{featurename
} to
\var{value
}. If the feature is not
160 recognized,
\exception{SAXNotRecognizedException
} is raised. If the
161 feature or its setting is not supported by the parser,
162 \var{SAXNotSupportedException
} is raised.
165 \begin{methoddesc
}[XMLReader
]{getProperty
}{propertyname
}
166 Return the current setting for property
\var{propertyname
}. If the
167 property is not recognized, a
\exception{SAXNotRecognizedException
}
168 is raised. The well-known propertynames are listed in the module
169 \module{xml.sax.handler
}.
172 \begin{methoddesc
}[XMLReader
]{setProperty
}{propertyname, value
}
173 Set the
\var{propertyname
} to
\var{value
}. If the property is not
174 recognized,
\exception{SAXNotRecognizedException
} is raised. If the
175 property or its setting is not supported by the parser,
176 \var{SAXNotSupportedException
} is raised.
180 \subsection{IncrementalParser Objects
181 \label{incremental-parser-objects
}}
183 Instances of
\class{IncrementalParser
} offer the following additional
186 \begin{methoddesc
}[IncrementalParser
]{feed
}{data
}
187 Process a chunk of
\var{data
}.
190 \begin{methoddesc
}[IncrementalParser
]{close
}{}
191 Assume the end of the
document. That will check well-formedness
192 conditions that can be checked only at the end, invoke handlers, and
193 may clean up resources allocated during parsing.
196 \begin{methoddesc
}[IncrementalParser
]{reset
}{}
197 This method is called after close has been called to reset the
198 parser so that it is ready to parse new documents. The results of
199 calling parse or feed after close without calling reset are
204 \subsection{Locator Objects
\label{locator-objects
}}
206 Instances of
\class{Locator
} provide these methods:
208 \begin{methoddesc
}[Locator
]{getColumnNumber
}{}
209 Return the column number where the current event ends.
212 \begin{methoddesc
}[Locator
]{getLineNumber
}{}
213 Return the line number where the current event ends.
216 \begin{methoddesc
}[Locator
]{getPublicId
}{}
217 Return the public identifier for the current event.
220 \begin{methoddesc
}[Locator
]{getSystemId
}{}
221 Return the system identifier for the current event.
225 \subsection{InputSource Objects
\label{input-source-objects
}}
227 \begin{methoddesc
}[InputSource
]{setPublicId
}{id
}
228 Sets the public identifier of this
\class{InputSource
}.
231 \begin{methoddesc
}[InputSource
]{getPublicId
}{}
232 Returns the public identifier of this
\class{InputSource
}.
235 \begin{methoddesc
}[InputSource
]{setSystemId
}{id
}
236 Sets the system identifier of this
\class{InputSource
}.
239 \begin{methoddesc
}[InputSource
]{getSystemId
}{}
240 Returns the system identifier of this
\class{InputSource
}.
243 \begin{methoddesc
}[InputSource
]{setEncoding
}{encoding
}
244 Sets the character encoding of this
\class{InputSource
}.
246 The encoding must be a string acceptable for an XML encoding
247 declaration (see section
4.3.3 of the XML recommendation).
249 The encoding attribute of the
\class{InputSource
} is ignored if the
250 \class{InputSource
} also contains a character stream.
253 \begin{methoddesc
}[InputSource
]{getEncoding
}{}
254 Get the character encoding of this InputSource.
257 \begin{methoddesc
}[InputSource
]{setByteStream
}{bytefile
}
258 Set the byte stream (a Python file-like object which does not
259 perform byte-to-character conversion) for this input source.
261 The SAX parser will ignore this if there is also a character stream
262 specified, but it will use a byte stream in preference to opening a
263 URI connection itself.
265 If the application knows the character encoding of the byte stream,
266 it should set it with the setEncoding method.
269 \begin{methoddesc
}[InputSource
]{getByteStream
}{}
270 Get the byte stream for this input source.
272 The getEncoding method will return the character encoding for this
273 byte stream, or None if unknown.
276 \begin{methoddesc
}[InputSource
]{setCharacterStream
}{charfile
}
277 Set the character stream for this input source. (The stream must be
278 a Python
1.6 Unicode-wrapped file-like that performs conversion to
281 If there is a character stream specified, the SAX parser will ignore
282 any byte stream and will not attempt to open a URI connection to the
286 \begin{methoddesc
}[InputSource
]{getCharacterStream
}{}
287 Get the character stream for this input source.
291 \subsection{AttributesImpl Objects
\label{attributes-impl-objects
}}
293 \class{AttributesImpl
} objects implement a portion of the mapping
294 protocol, and the methods
\method{copy()
},
\method{get()
},
295 \method{has_key()
},
\method{items()
},
\method{keys()
}, and
296 \method{values()
}. The following methods are also provided:
298 \begin{methoddesc
}[AttributesImpl
]{getLength
}{}
299 Return the number of attributes.
302 \begin{methoddesc
}[AttributesImpl
]{getNames
}{}
303 Return the names of the attributes.
306 \begin{methoddesc
}[AttributesImpl
]{getType
}{name
}
307 Returns the type of the attribute
\var{name
}, which is normally
311 \begin{methoddesc
}[AttributesImpl
]{getValue
}{name
}
312 Return the value of attribute
\var{name
}.
315 % getValueByQName, getNameByQName, getQNameByName, getQNames available
316 % here already, but documented only for derived class.
319 \subsection{AttributesNSImpl Objects
\label{attributes-ns-impl-objects
}}
321 \begin{methoddesc
}[AttributesNSImpl
]{getValueByQName
}{name
}
322 Return the value for a qualified name.
325 \begin{methoddesc
}[AttributesNSImpl
]{getNameByQName
}{name
}
326 Return the
\code{(
\var{namespace
},
\var{localname
})
} pair for a
327 qualified
\var{name
}.
330 \begin{methoddesc
}[AttributesNSImpl
]{getQNameByName
}{name
}
331 Return the qualified name for a
\code{(
\var{namespace
},
332 \var{localname
})
} pair.
335 \begin{methoddesc
}[AttributesNSImpl
]{getQNames
}{}
336 Return the qualified names of all attributes.