Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{xmllib}} |
| 2 | % Author: Sjoerd Mullender |
| 3 | \label{module-xmllib} |
| 4 | \stmodindex{xmllib} |
| 5 | \index{XML} |
| 6 | |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 7 | This module defines a class \class{XMLParser} which serves as the basis |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 8 | for parsing text files formatted in XML (eXtended Markup Language). |
| 9 | |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 10 | \begin{classdesc}{XMLParser}{} |
| 11 | The \class{XMLParser} class must be instantiated without arguments. |
| 12 | \end{classdesc} |
| 13 | |
| 14 | This class provides the following interface methods: |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 15 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 16 | \setindexsubitem{(XMLParser method)} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 17 | |
| 18 | \begin{funcdesc}{reset}{} |
| 19 | Reset the instance. Loses all unprocessed data. This is called |
| 20 | implicitly at the instantiation time. |
| 21 | \end{funcdesc} |
| 22 | |
| 23 | \begin{funcdesc}{setnomoretags}{} |
| 24 | Stop processing tags. Treat all following input as literal input |
| 25 | (CDATA). |
| 26 | \end{funcdesc} |
| 27 | |
| 28 | \begin{funcdesc}{setliteral}{} |
| 29 | Enter literal mode (CDATA mode). |
| 30 | \end{funcdesc} |
| 31 | |
| 32 | \begin{funcdesc}{feed}{data} |
| 33 | Feed some text to the parser. It is processed insofar as it consists |
| 34 | of complete elements; incomplete data is buffered until more data is |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 35 | fed or \method{close()} is called. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
| 38 | \begin{funcdesc}{close}{} |
| 39 | Force processing of all buffered data as if it were followed by an |
| 40 | end-of-file mark. This method may be redefined by a derived class to |
| 41 | define additional processing at the end of the input, but the |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 42 | redefined version should always call \method{close()}. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 45 | \begin{funcdesc}{translate_references}{data} |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 46 | Translate all entity and character references in \var{data} and |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 47 | returns the translated string. |
| 48 | \end{funcdesc} |
| 49 | |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 50 | \begin{funcdesc}{handle_xml}{encoding, standalone} |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 51 | This method is called when the \code{<?xml ...?>} tag is processed. |
| 52 | The arguments are the values of the encoding and standalone attributes |
| 53 | in the tag. Both encoding and standalone are optional. The values |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 54 | passed to \method{handle_xml()} default to \code{None} and the string |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 55 | \code{'no'} respectively. |
| 56 | \end{funcdesc} |
| 57 | |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 58 | \begin{funcdesc}{handle_doctype}{tag, data} |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 59 | This method is called when the \code{<!DOCTYPE...>} tag is processed. |
| 60 | The arguments are the name of the root element and the uninterpreted |
| 61 | contents of the tag, starting after the white space after the name of |
| 62 | the root element. |
| 63 | \end{funcdesc} |
| 64 | |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 65 | \begin{funcdesc}{handle_starttag}{tag, method, attributes} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 66 | This method is called to handle start tags for which a |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 67 | \code{start_\var{tag}()} method has been defined. The \var{tag} |
| 68 | argument is the name of the tag, and the \method{method} argument is the |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 69 | bound method which should be used to support semantic interpretation |
| 70 | of the start tag. The \var{attributes} argument is a dictionary of |
| 71 | attributes, the key being the \var{name} and the value being the |
| 72 | \var{value} of the attribute found inside the tag's \code{<>} brackets. |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 73 | Character and entity references in the \var{value} have |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 74 | been interpreted. For instance, for the tag |
| 75 | \code{<A HREF="http://www.cwi.nl/">}, this method would be called as |
Fred Drake | b0744c5 | 1997-12-29 19:59:38 +0000 | [diff] [blame] | 76 | \code{handle_starttag('A', self.start_A, \{'HREF': 'http://www.cwi.nl/'\})}. |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 77 | The base implementation simply calls \var{method} with \var{attributes} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 78 | as the only argument. |
| 79 | \end{funcdesc} |
| 80 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 81 | \begin{funcdesc}{handle_endtag}{tag, method} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 82 | This method is called to handle endtags for which an |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 83 | \code{end_\var{tag}()} method has been defined. The \var{tag} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 84 | argument is the name of the tag, and the |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 85 | \var{method} argument is the bound method which should be used to |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 86 | support semantic interpretation of the end tag. If no |
| 87 | \code{end_\var{tag}()} method is defined for the closing element, this |
| 88 | handler is not called. The base implementation simply calls |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 89 | \var{method}. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 90 | \end{funcdesc} |
| 91 | |
| 92 | \begin{funcdesc}{handle_data}{data} |
| 93 | This method is called to process arbitrary data. It is intended to be |
| 94 | overridden by a derived class; the base class implementation does |
| 95 | nothing. |
| 96 | \end{funcdesc} |
| 97 | |
| 98 | \begin{funcdesc}{handle_charref}{ref} |
| 99 | This method is called to process a character reference of the form |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 100 | \samp{\&\#\var{ref};}. \var{ref} can either be a decimal number, |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 101 | or a hexadecimal number when preceded by \code{x}. |
| 102 | In the base implementation, \var{ref} must be a number in the |
| 103 | range 0-255. It translates the character to \ASCII{} and calls the |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 104 | method \method{handle_data()} with the character as argument. If |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 105 | \var{ref} is invalid or out of range, the method |
| 106 | \code{unknown_charref(\var{ref})} is called to handle the error. A |
| 107 | subclass must override this method to provide support for character |
| 108 | references outside of the \ASCII{} range. |
| 109 | \end{funcdesc} |
| 110 | |
| 111 | \begin{funcdesc}{handle_entityref}{ref} |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 112 | This method is called to process a general entity reference of the |
| 113 | form \samp{\&\var{ref};} where \var{ref} is an general entity |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 114 | reference. It looks for \var{ref} in the instance (or class) |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 115 | variable \member{entitydefs} which should be a mapping from entity |
| 116 | names to corresponding translations. |
| 117 | If a translation is found, it calls the method \method{handle_data()} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 118 | with the translation; otherwise, it calls the method |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 119 | \code{unknown_entityref(\var{ref})}. The default \member{entitydefs} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 120 | defines translations for \code{\&}, \code{\&apos}, \code{\>}, |
| 121 | \code{\<}, and \code{\"}. |
| 122 | \end{funcdesc} |
| 123 | |
| 124 | \begin{funcdesc}{handle_comment}{comment} |
| 125 | This method is called when a comment is encountered. The |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 126 | \var{comment} argument is a string containing the text between the |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 127 | \samp{<!--} and \samp{-->} delimiters, but not the delimiters |
| 128 | themselves. For example, the comment \samp{<!--text-->} will |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 129 | cause this method to be called with the argument \code{'text'}. The |
| 130 | default method does nothing. |
| 131 | \end{funcdesc} |
| 132 | |
| 133 | \begin{funcdesc}{handle_cdata}{data} |
| 134 | This method is called when a CDATA element is encountered. The |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 135 | \var{data} argument is a string containing the text between the |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 136 | \samp{<![CDATA[} and \samp{]]>} delimiters, but not the delimiters |
| 137 | themselves. For example, the entity \samp{<![CDATA[text]]>} will |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 138 | cause this method to be called with the argument \code{'text'}. The |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 139 | default method does nothing, and is intended to be overridden. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 140 | \end{funcdesc} |
| 141 | |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 142 | \begin{funcdesc}{handle_proc}{name, data} |
| 143 | This method is called when a processing instruction (PI) is |
| 144 | encountered. The \var{name} is the PI target, and the \var{data} |
| 145 | argument is a string containing the text between the PI target and the |
| 146 | closing delimiter, but not the delimiter itself. For example, the |
| 147 | instruction \samp{<?XML text?>} will cause this method to be called |
| 148 | with the arguments \code{'XML'} and \code{'text'}. The default method |
| 149 | does nothing. Note that if a document starts with \code{<?xml |
| 150 | ...?>}, \method{handle_xml()} is called to handle it. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 151 | \end{funcdesc} |
| 152 | |
| 153 | \begin{funcdesc}{handle_special}{data} |
| 154 | This method is called when a declaration is encountered. The |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 155 | \var{data} argument is a string containing the text between the |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 156 | \samp{<!} and \samp{>} delimiters, but not the delimiters |
| 157 | themselves. For example, the entity \samp{<!ENTITY text>} will |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 158 | cause this method to be called with the argument \code{'ENTITY text'}. The |
| 159 | default method does nothing. Note that \code{<!DOCTYPE ...>} is |
| 160 | handled separately if it is located at the start of the document. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 161 | \end{funcdesc} |
| 162 | |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 163 | \begin{funcdesc}{syntax_error}{message} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 164 | This method is called when a syntax error is encountered. The |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 165 | \var{message} is a description of what was wrong. The default method |
| 166 | raises a \exception{RuntimeError} exception. If this method is |
| 167 | overridden, it is permissable for it to return. This method is only |
| 168 | called when the error can be recovered from. Unrecoverable errors |
| 169 | raise a \exception{RuntimeError} without first calling |
| 170 | \method{syntax_error()}. |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 171 | \end{funcdesc} |
| 172 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 173 | \begin{funcdesc}{unknown_starttag}{tag, attributes} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 174 | This method is called to process an unknown start tag. It is intended |
| 175 | to be overridden by a derived class; the base class implementation |
| 176 | does nothing. |
| 177 | \end{funcdesc} |
| 178 | |
| 179 | \begin{funcdesc}{unknown_endtag}{tag} |
| 180 | This method is called to process an unknown end tag. It is intended |
| 181 | to be overridden by a derived class; the base class implementation |
| 182 | does nothing. |
| 183 | \end{funcdesc} |
| 184 | |
| 185 | \begin{funcdesc}{unknown_charref}{ref} |
| 186 | This method is called to process unresolvable numeric character |
| 187 | references. It is intended to be overridden by a derived class; the |
| 188 | base class implementation does nothing. |
| 189 | \end{funcdesc} |
| 190 | |
| 191 | \begin{funcdesc}{unknown_entityref}{ref} |
| 192 | This method is called to process an unknown entity reference. It is |
| 193 | intended to be overridden by a derived class; the base class |
| 194 | implementation does nothing. |
| 195 | \end{funcdesc} |
| 196 | |
| 197 | Apart from overriding or extending the methods listed above, derived |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 198 | classes may also define methods and variables of the following form to |
| 199 | define processing of specific tags. Tag names in the input stream are |
| 200 | case dependent; the \var{tag} occurring in method names must be in the |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 201 | correct case: |
| 202 | |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 203 | \begin{funcdescni}{start_\var{tag}}{attributes} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 204 | This method is called to process an opening tag \var{tag}. The |
| 205 | \var{attributes} argument has the same meaning as described for |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 206 | \method{handle_starttag()} above. In fact, the base implementation of |
| 207 | \method{handle_starttag()} calls this method. |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 208 | \end{funcdescni} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 209 | |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 210 | \begin{funcdescni}{end_\var{tag}}{} |
Guido van Rossum | a10768a | 1997-11-18 15:11:22 +0000 | [diff] [blame] | 211 | This method is called to process a closing tag \var{tag}. |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 212 | \end{funcdescni} |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 213 | |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 214 | \begin{datadescni}{\var{tag}_attributes} |
Guido van Rossum | 02505e4 | 1998-01-29 14:55:24 +0000 | [diff] [blame] | 215 | If a class or instance variable \code{\var{tag}_attributes} exists, it |
| 216 | should be a list or a dictionary. If a list, the elements of the list |
| 217 | are the valid attributes for the element \var{tag}; if a dictionary, |
| 218 | the keys are the valid attributes for the element \var{tag}, and the |
| 219 | values the default values of the attributes, or \code{None} if there |
| 220 | is no default. |
| 221 | In addition to the attributes that were present in the tag, the |
Fred Drake | 3b5da76 | 1998-03-12 15:33:05 +0000 | [diff] [blame] | 222 | attribute dictionary that is passed to \method{handle_starttag()} and |
| 223 | \method{unknown_starttag()} contains values for all attributes that |
| 224 | have a default value. |
Fred Drake | 7f6e2c4 | 1998-02-13 14:38:23 +0000 | [diff] [blame] | 225 | \end{datadescni} |