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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
<!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.3. Parsing XML</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 9. XML Processing">
<link rel="previous" href="packages.html" title="9.2. Packages">
<link rel="next" href="unicode.html" title="9.4. Unicode">
</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> > <a href="../toc/index.html">Dive Into Python</a> > <a href="index.html">XML Processing</a> > <span class="thispage">Parsing XML</span></td>
<td id="navigation" align="right" valign="top"> <a href="packages.html" title="Prev: “Packages”"><<</a> <a href="unicode.html" title="Next: “Unicode”">>></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: </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.parse"></a>9.3. Parsing <span class="acronym">XML</span></h2>
</div>
</div>
<div></div>
</div>
<div class="abstract">
<p>As I was saying, actually parsing an <span class="acronym">XML</span> document is very simple: one line of code. Where you go from there is up to you.
</p>
</div>
<div class="example"><a name="d0e23245"></a><h3 class="title">Example 9.8. Loading an <span class="acronym">XML</span> document (for real this time)
</h3><pre class="screen">
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>from</span> xml.dom <span class='pykeyword'>import</span> minidom</span> <a name="kgp.parse.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">>>> </tt><span class="userinput">xmldoc = minidom.parse(<span class='pystring'>'~/diveintopython/common/py/kgp/binary.xml'</span>)</span> <a name="kgp.parse.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">>>> </tt><span class="userinput">xmldoc</span> <a name="kgp.parse.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput"><xml.dom.minidom.Document instance at 010BE87C></span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> xmldoc.toxml()</span> <a name="kgp.parse.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput"><?xml version="1.0" ?>
<grammar>
<ref id="bit">
<p>0</p>
<p>1</p>
</ref>
<ref id="byte">
<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
<xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
</ref>
</grammar></span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">As you saw in the <a href="packages.html" title="9.2. Packages">previous section</a>, this imports the <tt class="filename">minidom</tt> module from the <tt class="filename">xml.dom</tt> package.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Here is the one line of code that does all the work: <tt class="function">minidom.parse</tt> takes one argument and returns a parsed representation of the <span class="acronym">XML</span> document. The argument can be many things; in this case, it's simply a filename of an <span class="acronym">XML</span> document on my local disk. (To follow along, you'll need to change the path to point to your downloaded examples directory.)
But you can also pass a <a href="../file_handling/file_objects.html" title="6.2. Working with File Objects">file object</a>, or even a <a href="../html_processing/extracting_data.html#dialect.extract.urllib" title="Example 8.5. Introducing urllib">file-like object</a>. You'll take advantage of this flexibility later in this chapter.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The object returned from <tt class="function">minidom.parse</tt> is a <tt class="classname">Document</tt> object, a descendant of the <tt class="classname">Node</tt> class. This <tt class="classname">Document</tt> object is the root level of a complex tree-like structure of interlocking <span class="application">Python</span> objects that completely represent the <span class="acronym">XML</span> document you passed to <tt class="function">minidom.parse</tt>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><tt class="function">toxml</tt> is a method of the <tt class="classname">Node</tt> class (and is therefore available on the <tt class="classname">Document</tt> object you got from <tt class="function">minidom.parse</tt>). <tt class="function">toxml</tt> prints out the <span class="acronym">XML</span> that this <tt class="classname">Node</tt> represents. For the <tt class="classname">Document</tt> node, this prints out the entire <span class="acronym">XML</span> document.
</td>
</tr>
</table>
</div>
</div>
<p>Now that you have an <span class="acronym">XML</span> document in memory, you can start traversing through it.
</p>
<div class="example"><a name="kgp.parse.gettingchildnodes.example"></a><h3 class="title">Example 9.9. Getting child nodes</h3><pre class="screen">
<tt class="prompt">>>> </tt><span class="userinput">xmldoc.childNodes</span> <a name="kgp.parse.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">[<DOM Element: grammar at 17538908>]</span>
<tt class="prompt">>>> </tt><span class="userinput">xmldoc.childNodes[0]</span> <a name="kgp.parse.2.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput"><DOM Element: grammar at 17538908></span>
<tt class="prompt">>>> </tt><span class="userinput">xmldoc.firstChild</span> <a name="kgp.parse.2.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput"><DOM Element: grammar at 17538908></span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Every <tt class="classname">Node</tt> has a <tt class="function">childNodes</tt> attribute, which is a list of the <tt class="classname">Node</tt> objects. A <tt class="classname">Document</tt> always has only one child node, the root element of the <span class="acronym">XML</span> document (in this case, the <tt class="sgmltag-element">grammar</tt> element).
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.2.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">To get the first (and in this case, the only) child node, just use regular list syntax. Remember, there is nothing special
going on here; this is just a regular <span class="application">Python</span> list of regular <span class="application">Python</span> objects.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.2.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Since getting the first child node of a node is a useful and common activity, the <tt class="classname">Node</tt> class has a <tt class="function">firstChild</tt> attribute, which is synonymous with <tt class="literal">childNodes[0]</tt>. (There is also a <tt class="function">lastChild</tt> attribute, which is synonymous with <tt class="literal">childNodes[-1]</tt>.)
</td>
</tr>
</table>
</div>
</div>
<div class="example"><a name="d0e23458"></a><h3 class="title">Example 9.10. <tt class="function">toxml</tt> works on any node
</h3><pre class="screen">
<tt class="prompt">>>> </tt><span class="userinput">grammarNode = xmldoc.firstChild</span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> grammarNode.toxml()</span> <a name="kgp.parse.3.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput"><grammar>
<ref id="bit">
<p>0</p>
<p>1</p>
</ref>
<ref id="byte">
<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
<xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
</ref>
</grammar></span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.3.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Since the <tt class="function">toxml</tt> method is defined in the <tt class="classname">Node</tt> class, it is available on any <span class="acronym">XML</span> node, not just the <tt class="classname">Document</tt> element.
</td>
</tr>
</table>
</div>
</div>
<div class="example"><a name="kgp.parse.childnodescanbetext.example"></a><h3 class="title">Example 9.11. Child nodes can be text</h3><pre class="screen">
<tt class="prompt">>>> </tt><span class="userinput">grammarNode.childNodes</span> <a name="kgp.parse.4.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">[<DOM Text node "\n">, <DOM Element: ref at 17533332>, \
<DOM Text node "\n">, <DOM Element: ref at 17549660>, <DOM Text node "\n">]</span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> grammarNode.firstChild.toxml()</span> <a name="kgp.parse.4.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">
</span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> grammarNode.childNodes[1].toxml()</span> <a name="kgp.parse.4.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput"><ref id="bit">
<p>0</p>
<p>1</p>
</ref></span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> grammarNode.childNodes[3].toxml()</span> <a name="kgp.parse.4.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput"><ref id="byte">
<p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\
<xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p>
</ref></span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> grammarNode.lastChild.toxml()</span> <a name="kgp.parse.4.5"></a><img src="../images/callouts/5.png" alt="5" border="0" width="12" height="12">
<span class="computeroutput">
</span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.4.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Looking at the <span class="acronym">XML</span> in <tt class="filename">binary.xml</tt>, you might think that the <tt class="sgmltag-element">grammar</tt> has only two child nodes, the two <tt class="sgmltag-element">ref</tt> elements. But you're missing something: the carriage returns! After the <tt class="literal">'<grammar>'</tt> and before the first <tt class="literal">'<ref>'</tt> is a carriage return, and this text counts as a child node of the <tt class="sgmltag-element">grammar</tt> element. Similarly, there is a carriage return after each <tt class="literal">'</ref>'</tt>; these also count as child nodes. So <tt class="literal">grammar.childNodes</tt> is actually a list of 5 objects: 3 <tt class="classname">Text</tt> objects and 2 <tt class="classname">Element</tt> objects.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.4.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The first child is a <tt class="classname">Text</tt> object representing the carriage return after the <tt class="literal">'<grammar>'</tt> tag and before the first <tt class="literal">'<ref>'</tt> tag.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.4.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The second child is an <tt class="classname">Element</tt> object representing the first <tt class="sgmltag-element">ref</tt> element.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.4.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The fourth child is an <tt class="classname">Element</tt> object representing the second <tt class="sgmltag-element">ref</tt> element.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.4.5"><img src="../images/callouts/5.png" alt="5" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The last child is a <tt class="classname">Text</tt> object representing the carriage return after the <tt class="literal">'</ref>'</tt> end tag and before the <tt class="literal">'</grammar>'</tt> end tag.
</td>
</tr>
</table>
</div>
</div>
<div class="example"><a name="d0e23628"></a><h3 class="title">Example 9.12. Drilling down all the way to text</h3><pre class="screen">
<tt class="prompt">>>> </tt><span class="userinput">grammarNode</span>
<span class="computeroutput"><DOM Element: grammar at 19167148></span>
<tt class="prompt">>>> </tt><span class="userinput">refNode = grammarNode.childNodes[1]</span> <a name="kgp.parse.5.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">>>> </tt><span class="userinput">refNode</span>
<span class="computeroutput"><DOM Element: ref at 17987740></span>
<tt class="prompt">>>> </tt><span class="userinput">refNode.childNodes</span> <a name="kgp.parse.5.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">[<DOM Text node "\n">, <DOM Text node " ">, <DOM Element: p at 19315844>, \
<DOM Text node "\n">, <DOM Text node " ">, \
<DOM Element: p at 19462036>, <DOM Text node "\n">]</span>
<tt class="prompt">>>> </tt><span class="userinput">pNode = refNode.childNodes[2]</span>
<tt class="prompt">>>> </tt><span class="userinput">pNode</span>
<span class="computeroutput"><DOM Element: p at 19315844></span>
<tt class="prompt">>>> </tt><span class="userinput"><span class='pykeyword'>print</span> pNode.toxml()</span> <a name="kgp.parse.5.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput"><p>0</p></span>
<tt class="prompt">>>> </tt><span class="userinput">pNode.firstChild</span> <a name="kgp.parse.5.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput"><DOM Text node "0"></span>
<tt class="prompt">>>> </tt><span class="userinput">pNode.firstChild.data</span> <a name="kgp.parse.5.5"></a><img src="../images/callouts/5.png" alt="5" border="0" width="12" height="12">
<span class="computeroutput">u'0'</span></pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.5.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">As you saw in the previous example, the first <tt class="sgmltag-element">ref</tt> element is <tt class="literal">grammarNode.childNodes[1]</tt>, since childNodes[0] is a <tt class="classname">Text</tt> node for the carriage return.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.5.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The <tt class="sgmltag-element">ref</tt> element has its own set of child nodes, one for the carriage return, a separate one for the spaces, one for the <tt class="sgmltag-element">p</tt> element, and so forth.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.5.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">You can even use the <tt class="function">toxml</tt> method here, deeply nested within the document.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.5.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The <tt class="sgmltag-element">p</tt> element has only one child node (you can't tell that from this example, but look at <tt class="literal">pNode.childNodes</tt> if you don't believe me), and it is a <tt class="classname">Text</tt> node for the single character <tt class="literal">'0'</tt>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href="#kgp.parse.5.5"><img src="../images/callouts/5.png" alt="5" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The <tt class="literal">.data</tt> attribute of a <tt class="classname">Text</tt> node gives you the actual string that the text node represents. But what is that <tt class="literal">'u'</tt> in front of the string? The answer to that deserves its own section.
</td>
</tr>
</table>
</div>
</div>
</div>
<table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td width="35%" align="left"><br><a class="NavigationArrow" href="packages.html"><< Packages</a></td>
<td width="30%" align="center"><br> <span class="divider">|</span> <a href="index.html#kgp.divein" title="9.1. Diving in">1</a> <span class="divider">|</span> <a href="packages.html" title="9.2. Packages">2</a> <span class="divider">|</span> <span class="thispage">3</span> <span class="divider">|</span> <a href="unicode.html" title="9.4. Unicode">4</a> <span class="divider">|</span> <a href="searching.html" title="9.5. Searching for elements">5</a> <span class="divider">|</span> <a href="attributes.html" title="9.6. Accessing element attributes">6</a> <span class="divider">|</span> <a href="summary.html" title="9.7. Segue">7</a> <span class="divider">|</span>
</td>
<td width="35%" align="right"><br><a class="NavigationArrow" href="unicode.html">Unicode >></a></td>
</tr>
<tr>
<td colspan="3"><br></td>
</tr>
</table>
<div class="Footer">
<p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
</div>
</body>
</html>
|