File: attributes.html

package info (click to toggle)
diveintopython 5.4-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 4,116 kB
  • ctags: 2,838
  • sloc: python: 4,417; xml: 894; makefile: 29
file content (164 lines) | stat: -rw-r--r-- 15,349 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   
      <title>9.6.&nbsp;Accessing element attributes</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="Chapter&nbsp;9.&nbsp;XML Processing">
      <link rel="previous" href="searching.html" title="9.5.&nbsp;Searching for elements">
      <link rel="next" href="summary.html" title="9.7.&nbsp;Segue">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">You are here: <a href="../index.html">Home</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">XML Processing</a>&nbsp;&gt;&nbsp;<span class="thispage">Accessing element attributes</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="searching.html" title="Prev: &#8220;Searching for elements&#8221;">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="summary.html" title="Next: &#8220;Segue&#8221;">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">Dive Into Python</a></h1>
               <p id="tagline">Python from novice to pro</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=" "> <input type="submit" value="Search"><input type="hidden" name="cof" value="LW:752;L:http://diveintopython.org/images/diveintopython.png;LH:42;AH:left;GL:0;AWFID:3ced2bb1f7f1b212;"><input type="hidden" name="domains" value="diveintopython.org"><input type="hidden" name="sitesearch" value="diveintopython.org"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="en">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="kgp.attributes"></a>9.6.&nbsp;Accessing element attributes
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p><span class="acronym">XML</span> elements can have one or more attributes, and it is incredibly simple to access them once you have parsed an <span class="acronym">XML</span> document.
            </p>
         </div>
         <p>For this section, you'll be using the <tt class="filename">binary.xml</tt> grammar file that you saw in the <a href="searching.html" title="9.5.&nbsp;Searching for elements">previous section</a>.
         </p><a name="d0e24744"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="Note" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%">This section may be a little confusing, because of some overlapping terminology.  Elements in an <span class="acronym">XML</span> document have attributes, and <span class="application">Python</span> objects also have attributes.  When you parse an <span class="acronym">XML</span> document, you get a bunch of <span class="application">Python</span> objects that represent all the pieces of the <span class="acronym">XML</span> document, and some of these <span class="application">Python</span> objects represent attributes of the <span class="acronym">XML</span> elements.  But the (<span class="application">Python</span>) objects that represent the (<span class="acronym">XML</span>) attributes also have (<span class="application">Python</span>) attributes, which are used to access various parts of the (<span class="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>
         <div class="example"><a name="d0e24787"></a><h3 class="title">Example&nbsp;9.24.&nbsp;Accessing element attributes</h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">xmldoc = minidom.parse(<span class='pystring'>'binary.xml'</span>)</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">reflist = xmldoc.getElementsByTagName(<span class='pystring'>'ref'</span>)</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">bitref = reflist[0]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>print</span> bitref.toxml()</span>
<span class="computeroutput">&lt;ref id="bit"&gt;
  &lt;p&gt;0&lt;/p&gt;
  &lt;p&gt;1&lt;/p&gt;
&lt;/ref&gt;</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">bitref.attributes</span>          <a name="kgp.attributes.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">&lt;xml.dom.minidom.NamedNodeMap instance at 0x81e0c9c&gt;</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">bitref.attributes.keys()</span>   <a name="kgp.attributes.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"> <a name="kgp.attributes.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">[u'id']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">bitref.attributes.values()</span> <a name="kgp.attributes.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">[&lt;xml.dom.minidom.Attr instance at 0x81d5044&gt;]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">bitref.attributes[<span class='pystring'>"id"</span>]</span>    <a name="kgp.attributes.1.5"></a><img src="../images/callouts/5.png" alt="5" border="0" width="12" height="12">
<span class="computeroutput">&lt;xml.dom.minidom.Attr instance at 0x81d5044&gt;</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">Each <tt class="classname">Element</tt> object has an attribute called <tt class="literal">attributes</tt>, which is a <tt class="classname">NamedNodeMap</tt> object.  This sounds scary, but it's not, because a <tt class="classname">NamedNodeMap</tt> is an object that <a href="../object_oriented_framework/userdict.html" title="5.5.&nbsp;Exploring UserDict: A Wrapper Class">acts like a dictionary</a>, so you already know how to use it.
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">Treating the <tt class="classname">NamedNodeMap</tt> as a dictionary, you can get a list of the names of the attributes of this element by using <tt class="function">attributes.keys()</tt>.  This element has only one attribute, <tt class="literal">'id'</tt>.
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">Attribute names, like all other text in an <span class="acronym">XML</span> document, are stored in <a href="unicode.html" title="9.4.&nbsp;Unicode">unicode</a>.
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">Again treating the <tt class="classname">NamedNodeMap</tt> as a dictionary, you can get a list of the values of the attributes by using <tt class="function">attributes.values()</tt>.  The values are themselves objects, of type <tt class="classname">Attr</tt>.  You'll see how to get useful information out of this object in the next example.
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.1.5"><img src="../images/callouts/5.png" alt="5" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">Still treating the <tt class="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 <tt class="classname">NamedNodeMap</tt> class accomplishes this neat trick: by defining a <a href="../object_oriented_framework/special_class_methods.html" title="5.6.&nbsp;Special Class Methods"><tt class="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.)
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <div class="example"><a name="d0e24922"></a><h3 class="title">Example&nbsp;9.25.&nbsp;Accessing individual attributes</h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">a = bitref.attributes[<span class='pystring'>"id"</span>]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">a</span>
<span class="computeroutput">&lt;xml.dom.minidom.Attr instance at 0x81d5044&gt;</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">a.name</span>  <a name="kgp.attributes.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">u'id'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">a.value</span> <a name="kgp.attributes.2.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">u'bit'</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">The <tt class="classname">Attr</tt> object completely represents a single <span class="acronym">XML</span> attribute of a single <span class="acronym">XML</span> element.  The name of the attribute (the same name as you used to find this object in the <tt class="literal">bitref.attributes</tt> <tt class="classname">NamedNodeMap</tt> pseudo-dictionary) is stored in <tt class="literal">a.name</tt>.
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#kgp.attributes.2.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">The actual text value of this <span class="acronym">XML</span> attribute is stored in <tt class="literal">a.value</tt>.
                     </td>
                  </tr>
               </table>
            </div>
         </div><a name="d0e24990"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="Note" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%">Like a dictionary, attributes of an <span class="acronym">XML</span> element have no ordering.  Attributes may <span class="emphasis"><em>happen to be</em></span> listed in a certain order in the original <span class="acronym">XML</span> document, and the <tt class="classname">Attr</tt> objects may <span class="emphasis"><em>happen to be</em></span> listed in a certain order when the <span class="acronym">XML</span> document is parsed into <span class="application">Python</span> objects, but these orders are arbitrary and should carry no special meaning.  You should always access individual attributes
                  by name, like the keys of a dictionary.
               </td>
            </tr>
         </table>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="searching.html">&lt;&lt;&nbsp;Searching for elements</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#kgp.divein" title="9.1.&nbsp;Diving in">1</a> <span class="divider">|</span> <a href="packages.html" title="9.2.&nbsp;Packages">2</a> <span class="divider">|</span> <a href="parsing_xml.html" title="9.3.&nbsp;Parsing XML">3</a> <span class="divider">|</span> <a href="unicode.html" title="9.4.&nbsp;Unicode">4</a> <span class="divider">|</span> <a href="searching.html" title="9.5.&nbsp;Searching for elements">5</a> <span class="divider">|</span> <span class="thispage">6</span> <span class="divider">|</span> <a href="summary.html" title="9.7.&nbsp;Segue">7</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="summary.html">Segue&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright &copy; 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
      </div>
   </body>
</html>