<tdid="breadcrumb" colspan="5" align="left" valign="top">You are here: <ahref="http://www.diveintopython.net/index.html">Home</a> > <ahref="http://www.diveintopython.net/toc/index.html">Dive Into Python</a> > <ahref="http://www.diveintopython.net/xml_processing/index.html">XML Processing</a> > <spanclass="thispage">Accessing element attributes</span></td>
<tdid="navigation" align="right" valign="top"> <ahref="http://www.diveintopython.net/xml_processing/searching.html" title="Prev: “Searching for elements”"><<</a> <ahref="http://www.diveintopython.net/xml_processing/summary.html" title="Next: “Segue”">>></a></td>
</tr>
<tr>
<tdcolspan="3" id="logocontainer">
<h1id="logo"><ahref="http://www.diveintopython.net/index.html" accesskey="1">Dive Into Python</a></h1>
<h2class="title"><aname="kgp.attributes"></a>9.6. Accessing element attributes
</h2>
</div>
</div>
<div></div>
</div>
<divclass="abstract">
<p><spanclass="acronym">XML</span> elements can have one or more attributes, and it is incredibly simple to access them once you have parsed an <spanclass="acronym">XML</span> document.
</p>
</div>
<p>For this section, you'll be using the <ttclass="filename">binary.xml</tt> grammar file that you saw in the <ahref="http://www.diveintopython.net/xml_processing/searching.html" title="9.5. Searching for elements">previous section</a>.
<tdcolspan="2" align="left" valign="top" width="99%">This section may be a little confusing, because of some overlapping terminology. Elements in an <spanclass="acronym">XML</span> document have attributes, and <spanclass="application">Python</span> objects also have attributes. When you parse an <spanclass="acronym">XML</span> document, you get a bunch of <spanclass="application">Python</span> objects that represent all the pieces of the <spanclass="acronym">XML</span> document, and some of these <spanclass="application">Python</span> objects represent attributes of the <spanclass="acronym">XML</span> elements. But the (<spanclass="application">Python</span>) objects that represent the (<spanclass="acronym">XML</span>) attributes also have (<spanclass="application">Python</span>) attributes, which are used to access various parts of the (<spanclass="acronym">XML</span>) attribute that the object represents. I told you it was confusing. I am open to suggestions on how to distinguish these
more clearly.
</td>
</tr>
</table>
<divclass="example"><aname="d0e24787"></a><h3class="title">Example 9.24. Accessing element attributes</h3><preclass="screen">
<tdvalign="top" align="left">Each <ttclass="classname">Element</tt> object has an attribute called <ttclass="literal">attributes</tt>, which is a <ttclass="classname">NamedNodeMap</tt> object. This sounds scary, but it's not, because a <ttclass="classname">NamedNodeMap</tt> is an object that <ahref="http://www.diveintopython.net/object_oriented_framework/userdict.html" title="5.5. Exploring UserDict: A Wrapper Class">acts like a dictionary</a>, so you already know how to use it.
<tdvalign="top" align="left">Treating the <ttclass="classname">NamedNodeMap</tt> as a dictionary, you can get a list of the names of the attributes of this element by using <ttclass="function">attributes.keys()</tt>. This element has only one attribute, <ttclass="literal">'id'</tt>.
<tdvalign="top" align="left">Attribute names, like all other text in an <spanclass="acronym">XML</span> document, are stored in <ahref="http://www.diveintopython.net/xml_processing/unicode.html" title="9.4. Unicode">unicode</a>.
<tdvalign="top" align="left">Again treating the <ttclass="classname">NamedNodeMap</tt> as a dictionary, you can get a list of the values of the attributes by using <ttclass="function">attributes.values()</tt>. The values are themselves objects, of type <ttclass="classname">Attr</tt>. You'll see how to get useful information out of this object in the next example.
<tdvalign="top" align="left">Still treating the <ttclass="classname">NamedNodeMap</tt> as a dictionary, you can access an individual attribute by name, using normal dictionary syntax. (Readers who have been
paying extra-close attention will already know how the <ttclass="classname">NamedNodeMap</tt> class accomplishes this neat trick: by defining a <ahref="http://www.diveintopython.net/object_oriented_framework/special_class_methods.html" title="5.6. Special Class Methods"><ttclass="function">__getitem__</tt> special method</a>. Other readers can take comfort in the fact that they don't need to understand how it works in order to use it effectively.)
<tdvalign="top" align="left">The <ttclass="classname">Attr</tt> object completely represents a single <spanclass="acronym">XML</span> attribute of a single <spanclass="acronym">XML</span> element. The name of the attribute (the same name as you used to find this object in the <ttclass="literal">bitref.attributes</tt><ttclass="classname">NamedNodeMap</tt> pseudo-dictionary) is stored in <ttclass="literal">a.name</tt>.
<tdcolspan="2" align="left" valign="top" width="99%">Like a dictionary, attributes of an <spanclass="acronym">XML</span> element have no ordering. Attributes may <spanclass="emphasis"><em>happen to be</em></span> listed in a certain order in the original <spanclass="acronym">XML</span> document, and the <ttclass="classname">Attr</tt> objects may <spanclass="emphasis"><em>happen to be</em></span> listed in a certain order when the <spanclass="acronym">XML</span> document is parsed into <spanclass="application">Python</span> objects, but these orders are arbitrary and should carry no special meaning. You should always access individual attributes
<tdwidth="35%" align="left"><br/><aclass="NavigationArrow" href="http://www.diveintopython.net/xml_processing/searching.html"><< Searching for elements</a></td>