Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / module-xmllib.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="module-xml.sax.xmlreader.html" />
<link rel="parent" href="markup.html" />
<link rel="next" href="xml-namespace.html" />
<meta name='aesop' content='information' />
<title>13.13 xmllib -- A parser for XML documents</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.12.6 The AttributesNS Interface"
href="attributes-ns-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="13. Structured Markup Processing"
href="markup.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="13.13.1 XML Namespaces"
href="xml-namespace.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="attributes-ns-objects.html">13.12.6 The AttributesNS Interface</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">13. Structured Markup Processing</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="xml-namespace.html">13.13.1 XML Namespaces</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION00151300000000000000000">
13.13 <tt class="module">xmllib</tt> --
A parser for XML documents</A>
</H1>
<P>
<A NAME="module-xmllib"></A>
<P>
<a id='l2h-4588' xml:id='l2h-4588'></a>
<P>
<div class="versionnote"><b>Deprecated since release 2.0.</b>
Use <tt class="module"><a href="module-xml.sax.html">xml.sax</a></tt> instead. The newer XML
package includes full support for XML 1.0.</div><p></p>
<P>
<span class="versionnote">Changed in version 1.5.2:
Added namespace support.</span>
<P>
This module defines a class <tt class="class">XMLParser</tt> which serves as the basis
for parsing text files formatted in XML (Extensible Markup Language).
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-4562' xml:id='l2h-4562' class="class">XMLParser</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
The <tt class="class">XMLParser</tt> class must be instantiated without
arguments.<A NAME="tex2html163"
HREF="#foot66320"><SUP>13.1</SUP></A></dl>
<P>
This class provides the following interface methods and instance variables:
<P>
<dl><dt><b><tt id='l2h-4563' xml:id='l2h-4563' class="member">attributes</tt></b></dt>
<dd>
A mapping of element names to mappings. The latter mapping maps
attribute names that are valid for the element to the default value of
the attribute, or if there is no default to <code>None</code>. The default
value is the empty dictionary. This variable is meant to be
overridden, not extended since the default is shared by all instances
of <tt class="class">XMLParser</tt>.
</dl>
<P>
<dl><dt><b><tt id='l2h-4564' xml:id='l2h-4564' class="member">elements</tt></b></dt>
<dd>
A mapping of element names to tuples. The tuples contain a function
for handling the start and end tag respectively of the element, or
<code>None</code> if the method <tt class="method">unknown_starttag()</tt> or
<tt class="method">unknown_endtag()</tt> is to be called. The default value is the
empty dictionary. This variable is meant to be overridden, not
extended since the default is shared by all instances of
<tt class="class">XMLParser</tt>.
</dl>
<P>
<dl><dt><b><tt id='l2h-4565' xml:id='l2h-4565' class="member">entitydefs</tt></b></dt>
<dd>
A mapping of entitynames to their values. The default value contains
definitions for <code>'lt'</code>, <code>'gt'</code>, <code>'amp'</code>, <code>'quot'</code>,
and <code>'apos'</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4566' xml:id='l2h-4566' class="method">reset</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Reset the instance. Loses all unprocessed data. This is called
implicitly at the instantiation time.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4567' xml:id='l2h-4567' class="method">setnomoretags</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Stop processing tags. Treat all following input as literal input
(CDATA).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4568' xml:id='l2h-4568' class="method">setliteral</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Enter literal mode (CDATA mode). This mode is automatically exited
when the close tag matching the last unclosed open tag is encountered.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4569' xml:id='l2h-4569' class="method">feed</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Feed some text to the parser. It is processed insofar as it consists
of complete tags; incomplete data is buffered until more data is
fed or <tt class="method">close()</tt> is called.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4570' xml:id='l2h-4570' class="method">close</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Force processing of all buffered data as if it were followed by an
end-of-file mark. This method may be redefined by a derived class to
define additional processing at the end of the input, but the
redefined version should always call <tt class="method">close()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4571' xml:id='l2h-4571' class="method">translate_references</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Translate all entity and character references in <var>data</var> and
return the translated string.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4572' xml:id='l2h-4572' class="method">getnamespace</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a mapping of namespace abbreviations to namespace URIs that are
currently in effect.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4573' xml:id='l2h-4573' class="method">handle_xml</tt></b>(</nobr></td>
<td><var>encoding, standalone</var>)</td></tr></table></dt>
<dd>
This method is called when the "<tt class="samp">&lt;?xml ...?&gt;</tt>" tag is processed.
The arguments are the values of the encoding and standalone attributes
in the tag. Both encoding and standalone are optional. The values
passed to <tt class="method">handle_xml()</tt> default to <code>None</code> and the string
<code>'no'</code> respectively.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4574' xml:id='l2h-4574' class="method">handle_doctype</tt></b>(</nobr></td>
<td><var>tag, pubid, syslit, data</var>)</td></tr></table></dt>
<dd>
This<a id='l2h-4589' xml:id='l2h-4589'></a> method is called when the
"<tt class="samp">&lt;!DOCTYPE...&gt;</tt>" declaration is processed. The arguments are the
tag name of the root element, the Formal Public<a id='l2h-4590' xml:id='l2h-4590'></a> Identifier (or <code>None</code> if not specified), the system
identifier, and the uninterpreted contents of the internal DTD subset
as a string (or <code>None</code> if not present).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4575' xml:id='l2h-4575' class="method">handle_starttag</tt></b>(</nobr></td>
<td><var>tag, method, attributes</var>)</td></tr></table></dt>
<dd>
This method is called to handle start tags for which a start tag
handler is defined in the instance variable <tt class="member">elements</tt>. The
<var>tag</var> argument is the name of the tag, and the
<var>method</var> argument is the function (method) which should be used to
support semantic interpretation of the start tag. The
<var>attributes</var> argument is a dictionary of attributes, the key being
the <var>name</var> and the value being the <var>value</var> of the attribute
found inside the tag's <code>&lt;&gt;</code> brackets. Character and entity
references in the <var>value</var> have been interpreted. For instance,
for the start tag <code>&lt;A HREF="http://www.cwi.nl/"&gt;</code>, this method
would be called as <code>handle_starttag('A', self.elements['A'][0],
{'HREF': 'http://www.cwi.nl/'})</code>. The base implementation simply
calls <var>method</var> with <var>attributes</var> as the only argument.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4576' xml:id='l2h-4576' class="method">handle_endtag</tt></b>(</nobr></td>
<td><var>tag, method</var>)</td></tr></table></dt>
<dd>
This method is called to handle endtags for which an end tag handler
is defined in the instance variable <tt class="member">elements</tt>. The <var>tag</var>
argument is the name of the tag, and the <var>method</var> argument is the
function (method) which should be used to support semantic
interpretation of the end tag. For instance, for the endtag
<code>&lt;/A&gt;</code>, this method would be called as <code>handle_endtag('A',
self.elements['A'][1])</code>. The base implementation simply calls
<var>method</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4577' xml:id='l2h-4577' class="method">handle_data</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
This method is called to process arbitrary data. It is intended to be
overridden by a derived class; the base class implementation does
nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4578' xml:id='l2h-4578' class="method">handle_charref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process a character reference of the form
"<tt class="samp">&amp;#<var>ref</var>;</tt>". <var>ref</var> can either be a decimal number,
or a hexadecimal number when preceded by an "<tt class="character">x</tt>".
In the base implementation, <var>ref</var> must be a number in the
range 0-255. It translates the character to ASCII and calls the
method <tt class="method">handle_data()</tt> with the character as argument. If
<var>ref</var> is invalid or out of range, the method
<code>unknown_charref(<var>ref</var>)</code> is called to handle the error. A
subclass must override this method to provide support for character
references outside of the ASCII range.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4579' xml:id='l2h-4579' class="method">handle_comment</tt></b>(</nobr></td>
<td><var>comment</var>)</td></tr></table></dt>
<dd>
This method is called when a comment is encountered. The
<var>comment</var> argument is a string containing the text between the
"<tt class="samp">&lt;!-&#45;</tt>" and "<tt class="samp">-&#45;&gt;</tt>" delimiters, but not the delimiters
themselves. For example, the comment "<tt class="samp">&lt;!-&#45;text-&#45;&gt;</tt>" will
cause this method to be called with the argument <code>'text'</code>. The
default method does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4580' xml:id='l2h-4580' class="method">handle_cdata</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
This method is called when a CDATA element is encountered. The
<var>data</var> argument is a string containing the text between the
"<tt class="samp">&lt;![CDATA[</tt>" and "<tt class="samp">]]&gt;</tt>" delimiters, but not the delimiters
themselves. For example, the entity "<tt class="samp">&lt;![CDATA[text]]&gt;</tt>" will
cause this method to be called with the argument <code>'text'</code>. The
default method does nothing, and is intended to be overridden.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4581' xml:id='l2h-4581' class="method">handle_proc</tt></b>(</nobr></td>
<td><var>name, data</var>)</td></tr></table></dt>
<dd>
This method is called when a processing instruction (PI) is
encountered. The <var>name</var> is the PI target, and the <var>data</var>
argument is a string containing the text between the PI target and the
closing delimiter, but not the delimiter itself. For example, the
instruction "<tt class="samp">&lt;?XML text?&gt;</tt>" will cause this method to be called
with the arguments <code>'XML'</code> and <code>'text'</code>. The default method
does nothing. Note that if a document starts with "<tt class="samp">&lt;?xml
..?&gt;</tt>", <tt class="method">handle_xml()</tt> is called to handle it.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4582' xml:id='l2h-4582' class="method">handle_special</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
This method is called when a declaration is encountered. The
<var>data</var> argument is a string containing the text between the
"<tt class="samp">&lt;!</tt>" and "<tt class="samp">&gt;</tt>" delimiters, but not the delimiters
themselves. For example, the <a id='l2h-4591' xml:id='l2h-4591'></a>entity
declaration "<tt class="samp">&lt;!ENTITY text&gt;</tt>" will cause this method to be called
with the argument <code>'ENTITY text'</code>. The default method does
nothing. Note that "<tt class="samp">&lt;!DOCTYPE ...&gt;</tt>" is handled separately if it
is located at the start of the document.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4583' xml:id='l2h-4583' class="method">syntax_error</tt></b>(</nobr></td>
<td><var>message</var>)</td></tr></table></dt>
<dd>
This method is called when a syntax error is encountered. The
<var>message</var> is a description of what was wrong. The default method
raises a <tt class="exception">RuntimeError</tt> exception. If this method is
overridden, it is permissible for it to return. This method is only
called when the error can be recovered from. Unrecoverable errors
raise a <tt class="exception">RuntimeError</tt> without first calling
<tt class="method">syntax_error()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4584' xml:id='l2h-4584' class="method">unknown_starttag</tt></b>(</nobr></td>
<td><var>tag, attributes</var>)</td></tr></table></dt>
<dd>
This method is called to process an unknown start tag. It is intended
to be overridden by a derived class; the base class implementation
does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4585' xml:id='l2h-4585' class="method">unknown_endtag</tt></b>(</nobr></td>
<td><var>tag</var>)</td></tr></table></dt>
<dd>
This method is called to process an unknown end tag. It is intended
to be overridden by a derived class; the base class implementation
does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4586' xml:id='l2h-4586' class="method">unknown_charref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process unresolvable numeric character
references. It is intended to be overridden by a derived class; the
base class implementation does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4587' xml:id='l2h-4587' class="method">unknown_entityref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process an unknown entity reference. It is
intended to be overridden by a derived class; the base class
implementation calls <tt class="method">syntax_error()</tt> to signal an error.
</dl>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seetitle">
<dt><em class="citetitle"><a href="http://www.w3.org/TR/REC-xml"
>Extensible Markup Language
(XML) 1.0</a></em></dt>
<dd>The XML specification, published by the World
Wide Web Consortium (W3C), defines the syntax and
processor requirements for XML. References to additional
material on XML, including translations of the
specification, are available at
<a class="url" href="http://www.w3.org/XML/">http://www.w3.org/XML/</a>.</dd>
</dl>
<P>
<dl compact="compact" class="seetitle">
<dt><em class="citetitle"><a href="http://www.python.org/topics/xml/"
>Python and XML
Processing</a></em></dt>
<dd>The Python XML Topic Guide provides a great
deal of information on using XML from Python and links to
other sources of information on XML.</dd>
</dl>
<P>
<dl compact="compact" class="seetitle">
<dt><em class="citetitle"><a href="http://www.python.org/sigs/xml-sig/"
>SIG for XML
Processing in Python</a></em></dt>
<dd>The Python XML Special Interest
Group is developing substantial support for processing XML
from Python.</dd>
</dl>
</div>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot66320">...
arguments.</A><A
href="module-xmllib.html#tex2html163"><SUP>13.1</SUP></A></DT>
<DD>Actually, a number of keyword arguments are
recognized which influence the parser to accept certain non-standard
constructs. The following keyword arguments are currently
recognized. The defaults for all of these is <code>0</code> (false) except
for the last one for which the default is <code>1</code> (true).
<var>accept_unquoted_attributes</var> (accept certain attribute values
without requiring quotes), <var>accept_missing_endtag_name</var> (accept
end tags that look like <code>&lt;/&gt;</code>), <var>map_case</var> (map upper case to
lower case in tags and attributes), <var>accept_utf8</var> (allow UTF-8
characters in input; this is required according to the XML standard,
but Python does not as yet deal properly with these characters, so
this is not the default), <var>translate_attribute_references</var> (don't
attempt to translate character and entity references in attribute values).
</DD>
</DL>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="xml-namespace.html">13.13.1 XML Namespaces</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.12.6 The AttributesNS Interface"
href="attributes-ns-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="13. Structured Markup Processing"
href="markup.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="13.13.1 XML Namespaces"
href="xml-namespace.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="attributes-ns-objects.html">13.12.6 The AttributesNS Interface</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">13. Structured Markup Processing</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="xml-namespace.html">13.13.1 XML Namespaces</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>